Skip to content

Instantly share code, notes, and snippets.

@anutron
Created July 31, 2009 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anutron/159384 to your computer and use it in GitHub Desktop.
Save anutron/159384 to your computer and use it in GitHub Desktop.
var Templatizer = new Class({
Implements: [Options, Events],
options: {
templates: {
/*
name: instanceOfSubtleTemplate
*/
},
data: {}
},
initialize: function(options){
this.setOptions(options);
this.data = this.options.data;
this.addTemplates();
},
templates: {},
rendered: {},
addTemplate: function(name, subtleTemplate) {
return this.templates[name] = subtleTemplate;
},
addTemplates: function(templates) {
$each(templates || this.options.templates, function(element, name){
this.addTemplate(name, element);
}, this);
return this.templates;
},
update: function(data){
this.data = data;
$each(this.rendered, function(rendered, template){
rendered.populate(this.data);
this.fireEvent('update', [template, rendered, this]);
}, this);
},
//builds elements out from data
render: function(name) {
var rendered = this.rendered[name];
if (!rendered) {
var template = this.templates[name];
if (!template) return null;
rendered = this.rendered[name] = new template(this.data);
$(rendered).store('templatizer', this);
this.fireEvent('render', [name, rendered, this]);
}
return rendered;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment