Created
April 11, 2013 14:12
-
-
Save ghing/5363702 to your computer and use it in GitHub Desktop.
Backbone base view that handles compiling and retrieving templates.
This file contains hidden or 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
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