-
-
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)); |
Noticed this today as well, updated my Gist with fix. https://gist.github.com/funkjedi/5732611
@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.
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
@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.
Hi,
on line 37, templateId is undefined on this context, am i missing something ? how templateId get passed to compileTemplate function?