Create a gist now

Instantly share code, notes, and snippets.

Person.js (view)
YUI.add('personView',function(Y, NAME){
var TEMPLATES = {},
COMPILED_TEMPLATES = {};
Y.Object.each(TEMPLATES, function (template, name) {
COMPILED_TEMPLATES[name] = Y.Handlebars.compile(template);
});
Y.PersonView = Y.Base.create(
NAME,
Y.View,
[],
{
render:function(){
this.get('container').setHTML(COMPILED_TEMPLATES.person(this.get('model').getAttrs(['name','age','height'])) + COMPILED_TEMPLATES.another());
return this;
}
});
}, '0.0.1', {
requires: ['base-build','handlebars','node-base','view', 'personModel']
});
@Satyam
Owner
Satyam commented Jun 25, 2012

This new version is a little more generic since it allows for any number of templates in the static TEMPLATES object, and it compiles them all into this.templates. Actually, I see no reason not to compile them all into an equally static variable since, after all, they don't change. That'll go in a next version.

@Satyam
Owner
Satyam commented Jun 25, 2012

Ok, so, this has the compiled templates made static as well, no time wasted in doing it per-instance.

@Satyam
Owner
Satyam commented Jun 25, 2012

The render() method assumes there are two files under the Person.templates folder, one called person.html and the other another.html. That is why it refers to properties 'person' and 'another' in the COMPILED_TEMPLATES static variable.

Do not forget (as I did) the parenthesis after the compiled template name. They have to be called as a function even if there are no arguments to pass to them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment