Skip to content

Instantly share code, notes, and snippets.

@sashasimkin
Forked from funkjedi/marionette.handlebars.js
Last active December 19, 2015 01:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sashasimkin/5876792 to your computer and use it in GitHub Desktop.
Save sashasimkin/5876792 to your computer and use it in GitHub Desktop.
/**
NOTICE: This script is very obsolete and has some errors that are fixed in the original one.
Here it is: https://gist.github.com/funkjedi/5732611
And here is another fork with 2.x compatability: https://gist.github.com/hashchange/03d5da370a39863d5f7e
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 '[precompiled]';
}
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 (Handlebars.templates && Handlebars.templates[templateId]) {
return Handlebars.templates[templateId];
}
return Handlebars.compile(rawTemplate);
};
}(Handlebars, Marionette));
@aldwyish
Copy link

aldwyish commented Jul 2, 2013

Hi,
on line 37, templateId is undefined on this context, am i missing something ? how templateId get passed to compileTemplate function?

@funkjedi
Copy link

funkjedi commented Jul 2, 2013

Noticed this today as well, updated my Gist with fix. https://gist.github.com/funkjedi/5732611

@aldwyish
Copy link

aldwyish commented Jul 3, 2013

@funkjedi nice, i did a quick fix yesterday: https://gist.github.com/adwayish/5906937
but i think yours make more sense, mine passes the precompiled template to compileTemplate which doesn't sound right.

@Anachron
Copy link

Thanks a lot for this snippet!
Edit: Where is the context for template?
Edit2: This can't work, templateId is undefined in line 37 and context is missing in line 40.
Edit3: A mix of this and http://marionettejs.com/docs/backbone.marionette.html#section-151 would be the best in my opinion.
Edit4: Also this is a good help too: marionettejs/backbone.marionette#89

@hashchange
Copy link

@Anachron Just use the original gist, funkjedi/marionette.handlebars.js. Or take my own fork if you need it to be compatible with Marionette 2.x as well. Those work.

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