A few ways to bind a model to the DOM¶
1. Manual binding, applyBindings¶
html¶
<div id="app">
<input al-value="title" type="text" class="form-control" />
<p>{{title}}</p>
</div>
Make scope, then to bind it to the DOM with alight.applyBindings
code¶
var tag = document.querySelector('#app'); // take the tag
var scope = alight.Scope(); // make a Scope
scope.title = 'Hello!'; // set init value
alight.applyBindings(scope, tag); // apply bindings
2. Auto binding, al-app¶
alight.bootstrap is called on start system, it takes each element with al-app and execute alight.applyBindings
html¶
<div al-app al-init="title='Hello!'">
<input al-value="title" type="text" class="form-control" />
<p>{{title}}</p>
</div>
3. Manual binding with alight.bootstrap¶
You can bind custom elements with alight.bootstrap
html¶
<div id="app" al-init="title='Hello!'">
<input al-value="title" type="text" class="form-control" />
<p>{{title}}</p>
</div>
javascript¶
var tag = document.querySelector('#app'); // take the tag
alight.bootstrap([tag]); // bind to DOM
4. Deferred binding, alight.bootstrap¶
html¶
<div al-app al-init="title='Hello!'">
<input al-value="title" type="text" class="form-control" />
<p>{{title}}</p>
</div>
javascript¶
alight.autostart = false;
setTimeout(alight.bootstrap, 2000);
5. To bind to element with no DOM¶
html¶
<div id="app"></div>
javascript¶
var tag = document.createElement('div'); // make an element
// set up template
tag.innerHTML = '<input al-value="title" type="text" class="form-control" /><p>{{title}}</p>';
var scope = alight.Scope(); // make a scope
scope.title = 'Hello!'; // set init value
alight.applyBindings(scope, tag); // apply bindings
document.querySelector('#app').appendChild(tag); // append to DOM