Last active
December 19, 2015 05:49
-
-
Save aldwyish/5906937 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| 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)); |
Author
your welcome, never thought someone will find it useful, at least not this fast! You probably will do it this way if you peaked at Marionette.Renderer annotated code
Author
@jblanche check this one: https://gist.github.com/funkjedi/5732611
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Perfect, I did it another way (changing the load proto to pass the templateId to the compileTemplate function) but this way is cleaner.
Thanks :)