Created
February 24, 2011 22:40
-
-
Save alunny/843056 to your computer and use it in GitHub Desktop.
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
// lazy loading and caching templates | |
// callback receives rendered html - can write to the DOM or whatnot | |
var TMPL_PREFIX = 'tmpl.' | |
function renderTemplate(callback, identifier, data) { | |
// check localStorage for identifier | |
var data = data || {}, | |
key = TMPL_PREFIX + identifier, | |
url = 'templates/' + identifier + '.mustache', | |
tmpl; | |
if (tmpl = window.localStorage.getItem(key)) { | |
callback(Mustache.to_html(tmpl, data)); | |
} else { | |
x$().xhr(url, function () { | |
var tmpl = this.responseText; | |
window.localStorage.setItem(key, tmpl); | |
callback(Mustache.to_html(tmpl, data)); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment