Skip to content

Instantly share code, notes, and snippets.

@ghing
Created April 11, 2013 14:12
Show Gist options
  • Save ghing/5363702 to your computer and use it in GitHub Desktop.
Save ghing/5363702 to your computer and use it in GitHub Desktop.
Backbone base view that handles compiling and retrieving templates.
define([
"underscore",
"backbone",
"handlebars"
],
function(_, Backbone, Handlebars) {
var HandlebarsTemplateView = Backbone.View.extend({
templates: {},
initialize: function(options) {
this.compileTemplates();
},
compileTemplate: function(templateSource) {
return Handlebars.compile(templateSource);
},
compileTemplates: function() {
if (_.isObject(this.options.templateSource)) {
_.each(this.options.templateSource, function(templateSource, name ) {
this.templates[name] = this.compileTemplate(templateSource);
}, this);
}
else if (this.options.templateSource) {
this.templates['__main'] = this.compileTemplate(this.options.templateSource);
}
if (this.templates['__main']) {
this.template = this.templates['__main'];
}
},
getTemplate: function(name) {
if (name) {
return this.templates[name];
}
else {
return this.templates['__main'];
}
}
});
return HandlebarsTemplateView;
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment