Skip to content

Instantly share code, notes, and snippets.

@sunpietro
Last active February 10, 2016 06:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunpietro/6844110 to your computer and use it in GitHub Desktop.
Save sunpietro/6844110 to your computer and use it in GitHub Desktop.
Backbone.js template loader with caching
var loadTemplate = function (templateFilePath, templateData) {
//console.info('Loading: ' + templateFilePath + ' with data:', templateData);
if (!APP.cache.templates[templateFilePath]) {
var templateDir = 'js/app/template/';
var templateUrl = templateDir + templateFilePath + '.js';
var templateString = '';
$.ajax({
async : false,
dataType: 'text',
url : templateUrl,
success : function (response) {
templateString = response;
},
error : function (response) {
console.info('Error: ', response);
}
});
APP.cache.templates[templateFilePath] = _.template(templateString);
}
return APP.cache.templates[templateFilePath](templateData);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment