Created
March 12, 2012 10:13
-
-
Save danielmahal/2021037 to your computer and use it in GitHub Desktop.
Async template loading
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
// 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