Skip to content

Instantly share code, notes, and snippets.

@jasonmit
Created December 1, 2013 08:10
Show Gist options
  • Save jasonmit/7729770 to your computer and use it in GitHub Desktop.
Save jasonmit/7729770 to your computer and use it in GitHub Desktop.
Evented localStorage (used for data-binding)
var store = (function($) {
if(typeof(Storage) === 'undefined') {
throw new Error('This browser does not support localStorage')
}
var _$ = $({});
this.get = function(key) {
return localStorage.hasOwnProperty(key) ? localStorage[key] : false;
};
this.set = function(key, value) {
localStorage[key] = value;
_$.trigger.apply(_$, arguments);
};
this.on = function() {
_$.on.apply(_$, arguments);
};
this.off = function() {
_$.off.apply(_$, arguments);
};
return {
get: this.get,
set: this.set,
on: this.on,
off: this.off
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment