Skip to content

Instantly share code, notes, and snippets.

@Kieranties
Created November 20, 2011 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kieranties/1380258 to your computer and use it in GitHub Desktop.
Save Kieranties/1380258 to your computer and use it in GitHub Desktop.
A Simple LocalStorage Model
/*
Define a Settings model object
Properties are defined with getters and setters against the setting object itself.
*/
var settings = {
implement: function(){
var args = Array.prototype.slice.call(arguments); //take any number of arguments
args.forEach(function(name){
if(!this.hasOwnProperty(name)){ //only implement properties not already defined
Object.defineProperty(this, name, {
get: function(){
var val = localStorage[name];
if(val){
return JSON.parse(val);
}
return null;
},
set: function(val){
localStorage[name] = JSON.stringify(val);
}
});
}
}, this); //forEach allows setting of context, keep context of settings model
}
};
// implement settings
settings.implement("active", "disabled", "init");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment