Skip to content

Instantly share code, notes, and snippets.

@juanriaza
Created February 23, 2012 19:19
Show Gist options
  • Save juanriaza/1894463 to your computer and use it in GitHub Desktop.
Save juanriaza/1894463 to your computer and use it in GitHub Desktop.
var Data = {
_ownKeys: [],
_similar: [],
_appKey: 'listenAnywhere',
set: function(key, value)
{
var existingValue = localStorage.getItem(this._appKey);
if (null === existingValue) {
existingValue = {};
} else {
existingValue = JSON.parse(existingValue);
}
existingValue[key] = value;
try {
localStorage.setItem(this._appKey, JSON.stringify(existingValue));
} catch (e) {
if (e == QUOTA_EXCEEDED_ERR) {
localStorage.removeItem(this._appKey);
var temp = {};
temp[key] = value;
localStorage.setItem(this._appKey, temp);
this._ownKeys = [];
}
}
this._ownKeys.push(key);
},
get: function(key)
{
return JSON.parse(localStorage.getItem(this._appKey))[key];
},
has: function(key)
{
return contains(this._ownKeys, key);
},
// keep similar live and in localStorage
setSimilar: function(key, value) {
this._similar = value;
this.set(key, value);
},
hasSimilar: function(uri) {
return contains(this._similar, uri);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment