Last active
February 10, 2016 06:01
-
-
Save sunpietro/6844110 to your computer and use it in GitHub Desktop.
Backbone.js template loader with caching
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
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