Created
June 25, 2012 16:04
-
-
Save Satyam/2989480 to your computer and use it in GitHub Desktop.
Person.js (view)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] | |
}); |
Ok, so, this has the compiled templates made static as well, no time wasted in doing it per-instance.
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
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.