Skip to content

Instantly share code, notes, and snippets.

@danielmahal
Created March 12, 2012 10:13
Show Gist options
  • Save danielmahal/2021037 to your computer and use it in GitHub Desktop.
Save danielmahal/2021037 to your computer and use it in GitHub Desktop.
Async template loading
// TODO: Better caching. If there are two template requests at the same time, they will both do a get request.
var templateCache = {};
var template = function(name, callback) {
var template = templateCache[name];
if(!template) {
$.get('templates/' + name + '.html').success(function(data) {
template = templateCache[name] = _.template(data);
callback(template);
});
} else {
callback(template);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment