Reactive YUI is a set of YUI modules that brings reactive programming and data-binding support for YUI projects.
In order to start uing Reactive YUI, all you need to do is download the project and import the files in your HTML or configure YUI loader to know where are the files.
<script src="http://yui.yahooapis.com/3.13.0/build/yui/yui-min.js"></script>
<script src="reactive-yui/src/reactive-handlebars.js"></script>
<script src="reactive-yui/src/deps.js"></script>
<script src="reactive-yui/src/reactive.js"></script>
<script src="reactive-yui/src/reactive-model.js"></script>
<script src="reactive-yui/src/reactive-model-list.js"></script>
YUI({
modules: {
'reactive-handlebars': {
fullpath: 'reactive-yui/src/reactive-handlebars.js'
},
'deps': {
fullpath: 'reactive-yui/src/deps.js'
},
'reactive-model-list': {
fullpath: 'reactive-yui/src/reactive-model-list.js'
},
'reactive-model': {
fullpath: 'reactive-yui/src/reactive-model.js'
},
'reactive-view': {
fullpath: 'reactive-yui/src/reactive-view.js'
},
'reactive': {
fullpath: 'reactive-yui/src/reactive.js'
}
}
}).use('reactive-handlebars', funtion(){
// just use the modules
});
Reactive Handlebars, is a modified version of handlebars with support for rendering instances of YUI Attributes derivatives. It also integrates with the other reactive structures provided by Reactive YUI in order to auto-update values in the rendered template.
Let's suppose the following template:
<script id="template" type="text/handlebars">
<ul>
<li>
<strong>First Name:</strong>
<span>{{firstName}}</span>
</li>
<li>
<strong>Last Name:</strong>
<span>{{lastName}}</span>
</li>
<li>
<strong>Full Name:</strong>
<span>{{fullName}}</span>
</li>
</ul>
</script>
You can use Y.one
to grab the template contents, and use Y.ReactiveHandlebars.compile
to create a template just like you would do with Y.Handlebars.compile
. They have the same API
YUI().use('reactive-handlebars', 'reactive-model', 'node-base', function (Y) {
var person = new Y.ReactiveModel({
firstName: 'Anakin',
lastName: 'Skywalker',
})
var template = Y.ReactiveHandlebars.compile(Y.one('#template').getHTML());
Y.one('#container').setContent(template(person));
});
ReactiveHandlebars
is fully integration with YUI Attribute
module, wich means you can render instances of any class that derives on Attribute
without needing to serialize them.
var template = Y.Handlebars.compile(Y.one('#template').getHTML());
Y.one('#container').setContent(template(person.toJSON()));
var template = Y.ReactiveHandlebars.compile(Y.one('#template').getHTML());
Y.one('#container').setContent(template(person));
Wherever you update an attribute of a reactive structure used in your templates, the values on the template gets updated.
ReactiveHandlebars also knows how to update only the DOM parts that really needs to be updated. Don't ever get your entire template to get re-rendered again.
// this line will cause the rendered template to get updated.
person.set('lastName', 'Vader');
Note: ReactiveHandlebars inserts empty
<script>
tags in your rendered templates in order to identify the DOM elements that needs to be updated. This means you may need to replace:first-child
and:last-child
css selectors by:first-of-type()
and:last-of-type()
.