Created
November 20, 2011 13:14
-
-
Save Kieranties/1380258 to your computer and use it in GitHub Desktop.
A Simple LocalStorage Model
This file contains hidden or 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
/* | |
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