abstract localStorage a little

This commit is contained in:
cloudhead
2010-06-15 03:52:25 -04:00
parent 2b102afa69
commit 9b173bf6e4

View File

@@ -43,6 +43,7 @@ if (less.env === 'development') {
less.optimization = 3;
}
var cache = (typeof(window.localStorage) === 'undefined') ? null : window.localStorage;
//
// Select all links with the 'rel' attribute set to "less"
@@ -77,8 +78,8 @@ function loadStyleSheets(callback, async) {
}
function loadStyleSheet(sheet, callback, async) {
var css = typeof(localStorage) !== "undefined" && localStorage.getItem(sheet.href);
var timestamp = typeof(localStorage) !== "undefined" && localStorage.getItem(sheet.href + ':timestamp');
var css = cache && cache.getItem(sheet.href);
var timestamp = cache && cache.getItem(sheet.href + ':timestamp');
var styles = { css: css, timestamp: timestamp };
xhr(sheet.href, async, function (data, lastModified) {
@@ -140,9 +141,9 @@ function createCSS(styles, sheet, lastModified) {
}
// Don't update the local store if the file wasn't modified
if (lastModified && typeof(localStorage) !== "undefined") {
localStorage.setItem(sheet.href, styles);
localStorage.setItem(sheet.href + ':timestamp', lastModified);
if (lastModified && cache) {
cache.setItem(sheet.href, styles);
cache.setItem(sheet.href + ':timestamp', lastModified);
}
}