Skip to content

Instantly share code, notes, and snippets.

@aldwyish
Last active December 19, 2015 05:49
Show Gist options
  • Select an option

  • Save aldwyish/5906937 to your computer and use it in GitHub Desktop.

Select an option

Save aldwyish/5906937 to your computer and use it in GitHub Desktop.
/**
Usage: Just include this script after Marionette and Handlebars loading
IF you use require.js add script to shim and describe it in the requirements
*/
(function(Handlebars, Marionette) {
Marionette.Handlebars = {
path: 'templates/',
extension: '.handlebars'
};
Marionette.TemplateCache.prototype.loadTemplate = function(templateId) {
var template, templateUrl;
if (Handlebars.templates && Handlebars.templates[templateId]) {
return Handlebars.templates[templateId];
}
try{
template = Marionette.$(templateId).html();
} catch(e) {}
if (!template || template.length === 0) {
templateUrl = Marionette.Handlebars.path + templateId + Marionette.Handlebars.extension;
Marionette.$.ajax({
url: templateUrl,
success: function(data) {
template = data;
},
async: false
});
if (!template || template.length === 0){
throw "NoTemplateError - Could not find template: '" + templateUrl + "'";
}
}
return template;
};
Marionette.TemplateCache.prototype.compileTemplate = function(rawTemplate) {
if (typeof rawTemplate === "function"){
return rawTemplate;
}
else {
return Handlebars.compile(rawTemplate);
}
};
}(Handlebars, Marionette));
@aldwyish
Copy link
Author

aldwyish commented Jul 3, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment