localStorage fallback

This commit is contained in:
Tim Petricola
2014-03-11 11:52:02 -04:00
parent fda0e8405f
commit d6da8d9925
3 changed files with 23 additions and 23 deletions
+20 -13
View File
@@ -1,24 +1,31 @@
window.fakeStorage = {
_data : {},
setItem : function (id, val) {
console.log('set');
return this._data[id] = String(val);
},
getItem : function (id) {
return this._data.hasOwnProperty(id) ? this._data[id] : undefined;
},
removeItem : function (id) { return delete this._data[id]; },
clear : function () { return this._data = {}; }
};
function LocalScoreManager() {
var localSupported = !!window.localStorage;
this.key = 'bestScore';
this.storage = localSupported ? window.localStorage : window.fakeStorage;
}
LocalScoreManager.prototype.get = function () {
if (!this.isSupported()) {
return 0;
var score = this.storage.getItem(this.key);
if (typeof score === "undefined" || score === null) {
score = 0;
}
return localStorage.getItem(this.key);
return score;
};
LocalScoreManager.prototype.set = function (score) {
if (!this.isSupported()) {
return false;
}
localStorage.setItem(this.key, score);
};
LocalScoreManager.prototype.isSupported = function () {
return !!window.localStorage;
this.storage.setItem(this.key, score);
};