Skip to content

Instantly share code, notes, and snippets.

@mspanish
Created August 11, 2013 05:44
Show Gist options
  • Save mspanish/6203550 to your computer and use it in GitHub Desktop.
Save mspanish/6203550 to your computer and use it in GitHub Desktop.
/**
* 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