Skip to content

Instantly share code, notes, and snippets.

@johnpolacek
Created June 2, 2017 12:10
Show Gist options
  • Save johnpolacek/6ae58bd2a4c19c9fd25907f018543cbe to your computer and use it in GitHub Desktop.
Save johnpolacek/6ae58bd2a4c19c9fd25907f018543cbe to your computer and use it in GitHub Desktop.
Load and cache multiple external jsrender templates
function loadTemplates() {
var toLoad = [],
loadAll = $.Deferred();
$.each(arguments, function(i, templateName) {
var filepath = '/path/to/templates/' + templateName + '.html',
loadTemplate = $.Deferred();
toLoad.push(loadTemplate);
if ($.templates[templateName]) {
loadTemplate.resolve();
} else {
$.get(filepath , function(html) {
var newTemplate = {};
newTemplate[templateName] = html;
$.templates(newTemplate);
}).fail(function() {
throw 'Could not load template at '+filepath;
}).done(function() {
loadTemplate.resolve();
});
}
})
$.when.apply($, toLoad).done(function() {
loadAll.resolve();
});
return loadAll;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment