Skip to content

Instantly share code, notes, and snippets.

@CarlosBonetti
Last active August 29, 2015 14:03
Show Gist options
  • Select an option

  • Save CarlosBonetti/17f70a3333717a0888e6 to your computer and use it in GitHub Desktop.

Select an option

Save CarlosBonetti/17f70a3333717a0888e6 to your computer and use it in GitHub Desktop.
Persistent Configurations for user browser-based options persistence
/**
* 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