Created
August 11, 2013 05:44
-
-
Save mspanish/6203550 to your computer and use it in GitHub Desktop.
Customizing template loading in Marionette
From https://raw.github.com/trupin/backbone.marionette-blog-sample/master/public/src/customizations.js
This file contains 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
/** | |
* User: rupin_t | |
* Date: 6/27/13 | |
* Time: 5:42 PM | |
*/ | |
/** | |
* Custom the template loading. | |
* Each template is downloaded from the server and the cached. | |
* @param templateId | |
*/ | |
Backbone.Marionette.TemplateCache.prototype.loadTemplate = function (templateId) { | |
if (!templateId) return ''; | |
if (_.isArray(templateId)) { | |
var template = ''; | |
templateId = '/modules/' + templateId[0] + '/html/' + templateId[1] + '.html'; | |
$.ajax(templateId, { async: false }).done(function (data) { template = data; }); | |
return template; | |
} | |
else if (_.isString(templateId)) { | |
var $t = $(templateId); | |
if ($t.length) | |
return $t.html(); | |
} | |
return ''; | |
}; | |
/** | |
* Custom the template rendering. | |
* Use Handlebars instead of _.template | |
* @param rawTemplate | |
* @returns {*} | |
*/ | |
Backbone.Marionette.TemplateCache.prototype.compileTemplate = function(rawTemplate) { | |
return Handlebars.compile(rawTemplate); | |
}; | |
/** | |
* Custom the renderer. | |
* The renderer will now use the TemplateCache of Marionette | |
* @param template | |
* @param data | |
*/ | |
Backbone.Marionette.Renderer.render = function(template, data){ | |
return Marionette.TemplateCache.get(template)(data); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment