Menu

Scopes

Csaba Skrabák

The names that appear in the [Placeholders] are primarily meant in the narrowest scope they can be found in. The scopes in order of specificity are as follows:

  1. iteration block - name belongs to the iterator value of the template
  2. component - component backing class fields
  3. page - access global js variables (window)
  4. built-in utility functions and variables of the templating engine
  5. fallback API - access backend with a protocol specified in app configuration
/*
(this belongs to the engine.js) transplate.node.js script should generate 
<script> elements describing such scopes. The in-browser template engine
would build a hierarchy of scopes.
*/
class Scope {
  constructor(parentScope) {
    this.map = {}
    this.parentScope = parentScope
  }

  // a block is entered, which may have local definitions
  //  - the html page
  //  - an html template
  //  - a custom component
  enter() {
    return new Scope(this)
  }

  // a block is exited, need to remove its local definitions from the scope
  exit() {
    return parentScope
  }

  // add a definition that is local to the current block
  add(name, getter) {
    this.map[name] = getter
  }

  // get the getter for a name; consider the innermost one if overdefined
  getGetter(name) {
    for (var s = this; s != null; s = s.parentScope) {
      if (name in s.map) {
        return s.map[name]
      }
    }
  }
}

Related

Wiki: Home
Wiki: Placeholders