Created
March 18, 2011 14:05
-
-
Save jsocol/876117 to your computer and use it in GitHub Desktop.
Simple namespaced storage API for JS (largely stolen from Chris Van)
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
/** | |
* storage.js - Simple namespaced browser storage. | |
* | |
* Creates a window.Storage function that gives you an easy API to access localStorage, | |
* with fallback to cookie storage. Each Storage object is namespaced: | |
* | |
* var foo = Storage('foo'), bar = Storage('bar'); | |
* foo.set('test', 'A'); bar.set('test', 'B'); | |
* foo.get('test'); // 'A' | |
* bar.remove('test'); | |
* foo.get('test'); // still 'A' | |
* | |
* Requires jQuery. | |
* Based on https://github.com/jbalogh/zamboni/blob/master/media/js/zamboni/storage.js | |
* Everything clever written by Chris Van. | |
*/ | |
var Storage = (function () { | |
var cookieStorage = { | |
expires: 30, | |
getItem: function (key) { | |
return $.cookie(key); | |
}, | |
setItem: function (key, value) { | |
return $.cookie(key, value, {path: "/", expires: this.expires}); | |
}, | |
removeItem: function (key) { | |
return $.cookie(key, null); | |
} | |
}; | |
var engine = cookieStorage; | |
try { | |
if ("localStorage" in window && window["localStorage"] !== null) { | |
engine = window.localStorage; | |
} | |
} catch (e) { | |
} | |
return function (namespace) { | |
if (!namespace) { | |
namespace = ''; | |
} | |
return { | |
get: function (key) { | |
return engine.getItem(namespace + "-" + key); | |
}, | |
set: function (key, value) { | |
return engine.setItem(namespace + "-" + key, value); | |
}, | |
remove: function (key) { | |
return engine.remoteItem(namespace + "-" + key); | |
} | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the remove function has a misspelling. It is returning engine.remoTe, instead of engine.remoVe