Data binding
Declarative data-bind attributes, models your game publishes, and how an update reaches the page. Directives, expressions, lists and their limits.
Data binding lets a page declare what shows a value, while the host or the page's script owns the
value. The page carries data-bind-* attributes, a model is a plain JavaScript object in the page,
and the engine's DataBindEngine keeps the DOM in step when the model changes. A test page covers
the value, if, class, style, checked and for directives.
Scripting lists the calls that publish and refresh a model. This page is the reference for what the attributes do.
An example
<span data-bind-value="{{player.name}}"></span>
<div data-bind-if="{{player.alive}}">Alive</div>
<div class="bar" data-bind-style-width="{{player.hp + '%'}}"></div>
<div class="badge" data-bind-class="{low: {{player.hp < 30}}, gold: {{player.level >= 10}}}"></div>
<ul data-bind-for="item of {{player.inventory}}">
<li>{{index}}. {{item.name}} x{{item.qty}}</li>
</ul>view.setDataModel("player",
R"({"name":"Ada","hp":72,"level":7,"alive":true,"inventory":[{"name":"Potion","qty":2}]})");setDataModel parses the JSON inside V8, makes the result the global player, and re-evaluates
every binding that reads player. Nothing is compiled from a source string.
Models
A model is a global variable in the page's script context. It can come from the host
(setDataModel(name, json)), or from page script (window.player = { ... }). To change it:
- From the host, call
setDataModelagain to replace the model, ornotifyDataModelUpdatedafter the model was edited in place. - From the page, edit the object and call
ApertureDataBind.update("player"), or replace it withApertureDataBind.set("player", newObject).
player.hp -= 5;
ApertureDataBind.update("player");Until a model exists, a binding that reads it evaluates to an empty string. Script errors inside an expression do the same, so a typo shows as empty text and not as an exception.
All calls into the view, including these, belong on the thread that ticks it (see Threads).
Directives
| Attribute | Effect |
|---|---|
data-bind-value | Sets the element's text content |
data-bind-if | Shows or hides the element. A false value adds inline display: none. The attribute data-bind-hidden is set to true or false, so CSS can match [data-bind-hidden="true"]. |
data-bind-class | {name: {{expr}}, other: {{expr}}}. Each pair adds or removes one class. |
data-bind-style-NAME | Sets one inline style property, where NAME is a CSS property name such as width or background-color. An empty value removes it. |
data-bind-attr-NAME | Sets any attribute to the value |
data-bind-src, data-bind-href | Set src and href |
data-bind-checked | Adds or removes the checked attribute of a checkbox or radio button |
data-bind-for | item of {{expr}}. The markup inside the element is a template that is repeated once per item. Put the attribute on the container (<ul>), not on the item (<li>). |
Every effect is applied through the DOM, so the cascade, layout and script observers all see it.
The value of the attribute must be one {{ ... }} expression, and the expression has to begin with
the model name (see below). Text that follows the value can go inside the expression:
{{player.hp + '%'}}, not {{player.hp}}%. Text that comes before the value goes in the markup
instead, next to the bound element: Lv <span data-bind-value="{{player.level}}"></span>.
data-bind-class is the exception to the one-expression rule: it holds a { ... } object with one
{{ ... }} per class.
Expressions
An expression is ordinary JavaScript, evaluated in the page's context against its globals, so
{{player.hp < 30}} and {{player.hp.toFixed(0)}} both work. All the bindings that an update
touches are evaluated in a single script run. The result is turned into a string:
data-bind-ifanddata-bind-classtreat the empty string,0,false,nullandundefinedas false, and everything else as true.data-bind-checkedtreats the empty string,0andfalseas false.- An empty array turns into the empty string, so it counts as false. Compare the length, as in
player.inventory.length !== 0, when you mean "has items".
Begin every expression with the model name
The engine decides which model a binding reads from the text before the first . of the
expression, or the whole expression when it has no dot. An update to player re-evaluates only the
bindings filed under player. {{player.hp}} and {{player.hp < 30}} qualify. {{!player.alive}}
is filed under !player and {{'Lv ' + player.level}} under 'Lv ' + player. Both render
correctly the first time but are not refreshed by setDataModel("player", ...) or
ApertureDataBind.update("player"). Write them as {{player.alive === false}} and as markup text
around a bound element, Lv <span data-bind-value="{{player.level}}"></span>, or refresh every
binding with synchronizeAllBindings() or ApertureDataBind.rescan(). A call such as
Math.round(player.hp) is filed under Math, so it has the same problem: keep the model first,
as in player.hp.toFixed(0).
Lists
data-bind-for="item of {{player.inventory}}" takes the markup inside the element, as it was when the
document was scanned, as a template. For each item it builds that markup again and replaces the
element's children with the result. The element itself appears once. Inside the template:
{{item.name}}reads a path on the current item, using the name beforeof.{{index}}is the zero-based position.- Any other
{{ ... }}is evaluated against the page's globals.
The collection can be an array or an object, whose values are used. A list is rebuilt from the
template on each update, with no diffing, and the children are replaced only when the generated
markup differs from what is there. data-bind-* attributes inside a repeated template are not
bound, and the substitution is what fills it.
When bindings are found
The engine scans the document for data-bind-* attributes each time a loadHtmlFromFile,
loadHtmlFromString or loadHtmlFromUrl call finishes, and then evaluates every binding once. A
binding that is added later, for example by script that inserts new markup, is not seen until you
rescan with view.rescanDataBindings() or ApertureDataBind.rescan(). If a script defines or
changes a model after the scan, call ApertureDataBind.update(name) to publish it.
Do not rescan after a list has rendered
A rescan reads each data-bind-for template again from the element's children as they are at
that moment. Once a list has rendered, those children are the rendered items, not the template, so
the list no longer follows its model. Use setDataModel and ApertureDataBind.update to refresh,
and rescan only for pages whose lists have not rendered yet.
Limits
- One-way only. A change in an input does not write back to the model. Listen for
inputorchangeand update the model yourself. - Updates are per model, not per frame. Publish when the data changes, and not on every tick. See Scripting performance.
- Refresh is keyed by model name. The engine does not track which values an expression reads,
only the name before its first
..