Last active
August 29, 2015 14:03
-
-
Save CarlosBonetti/17f70a3333717a0888e6 to your computer and use it in GitHub Desktop.
Persistent Configurations for user browser-based options persistence
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
| /** | |
| * Store configurations in the localStorage (HTML5 compatible browsers only) | |
| * so the user does not have to set basic options every time the browser are reloaded | |
| */ | |
| var PersistentConfig = { | |
| /** | |
| * The namespace prefix that will be applied to all storage keys, so all persistent config are kept on | |
| * the same namespace | |
| */ | |
| prefix: "persistentconfig_", | |
| /** | |
| * Return a new string with the prefixed namespace included | |
| */ | |
| applyPrefix: function (key) { | |
| return this.prefix + key; | |
| }, | |
| /** | |
| * Return the stored configuration with the name `key` or `default_value` if not found or browser is not compatible | |
| */ | |
| get: function (key, default_value) { | |
| return localStorage.getItem(this.applyPrefix(key)) || default_value; | |
| }, | |
| /** | |
| * Store the configuration | |
| */ | |
| set: function (key, value) { | |
| return localStorage.setItem(this.applyPrefix(key), value); | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment