Skip to content

Instantly share code, notes, and snippets.

@Satyam
Created June 25, 2012 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Satyam/2989480 to your computer and use it in GitHub Desktop.
Save Satyam/2989480 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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