Created
September 6, 2012 10:11
-
-
Save aheinze/3654347 to your computer and use it in GitHub Desktop.
Local+Session sorage wrapper
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
;(function(ls, ss){ | |
var fallback = {setItem:function(){}, getItem:function(){ return null;}, removeItem:function(){}}, | |
ls = ls || fallback}, | |
ss = ss || fallback, | |
JSON = JSON || {stringify:function(){},parse:function(){}}; | |
window.Store = { | |
"set": function(key, value) { | |
ls.setItem(key, JSON.stringify(value)); | |
}, | |
"get": function(key, def) { | |
var val = ls.getItem(key); | |
return val ? JSON.parse(val):def; | |
}, | |
"remove": function() { | |
ls.removeItem(key); | |
}, | |
"persist": function(key, value) { | |
ss.setItem(key, JSON.stringify(value)); | |
}, | |
"remember": function(key, def) { | |
var val = ss.getItem(key); | |
return val ? JSON.parse(val):def; | |
}, | |
"forget": function(key) { | |
ss.removeItem(key); | |
} | |
}; | |
})(window["localStorage"], window["sessionStorage"]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment