Created
June 2, 2017 12:10
-
-
Save johnpolacek/6ae58bd2a4c19c9fd25907f018543cbe to your computer and use it in GitHub Desktop.
Load and cache multiple external jsrender templates
This file contains hidden or 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
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