Skip to content

Instantly share code, notes, and snippets.

@chrishoage
Last active December 15, 2015 07:39
Show Gist options
  • Save chrishoage/5225216 to your computer and use it in GitHub Desktop.
Save chrishoage/5225216 to your computer and use it in GitHub Desktop.
Backbone.Marionette.TemplateCache.prototype.loadTemplate = function(templateId){
// load your template here, returning a the data needed for the compileTemplate
// function. For example, you have a function that creates templates based on the
// value of templateId
if (Handlebars.templates === undefined || Handlebars.templates[templateId] === undefined) {
var deffered = $.ajax({
url : '/templates/' + templateId + '.handlebars',
dataType : 'text',
success : function(data) {
if (Handlebars.templates === undefined) {
Handlebars.templates = {};
}
Handlebars.templates[templateId] = Handlebars.compile(data);
}
});
}
// send the template back
return $.when(Handlebars.templates[templateId] || deffered);
}
Backbone.Marionette.TemplateCache.prototype.compileTemplate = function(rawTemplate) {
// use Handlebars.js to compile the template
return _.isFunction(rawTemplate) ? rawTemplate : Handlebars.compile(rawTemplate);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment