Skip to content

Instantly share code, notes, and snippets.

@hashchange
Forked from funkjedi/marionette.handlebars.js
Last active August 29, 2015 14:02
Show Gist options
  • Save hashchange/03d5da370a39863d5f7e to your computer and use it in GitHub Desktop.
Save hashchange/03d5da370a39863d5f7e to your computer and use it in GitHub Desktop.
Handlebars for Marionette. -- No longer maintained. Instead, please use https://github.com/hashchange/marionette.handlebars
/**
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
*
* This gist is no longer updated. Please use
*
* https://github.com/hashchange/marionette.handlebars
*
* instead.
*
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
*
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, Backbone) {
var Marionette = Backbone.Marionette;
Marionette.Handlebars = {
path: 'templates/',
extension: '.handlebars'
};
Marionette.TemplateCache.prototype.load = function() {
if (this.compiledTemplate) {
return this.compiledTemplate;
}
if (Handlebars.templates && Handlebars.templates[this.templateId]) {
this.compiledTemplate = Handlebars.templates[this.templateId];
}
else {
var template = this.loadTemplate(this.templateId);
this.compiledTemplate = this.compileTemplate(template);
}
return this.compiledTemplate;
};
Marionette.TemplateCache.prototype.loadTemplate = function(templateId) {
var template, templateUrl;
try {
template = Backbone.$(templateId).html();
}
catch (e) {}
if (!template || template.length === 0) {
templateUrl = Marionette.Handlebars.path + templateId + Marionette.Handlebars.extension;
Backbone.$.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) {
return Handlebars.compile(rawTemplate);
};
}(Handlebars, Backbone));
@hashchange
Copy link
Author

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