Created
July 24, 2013 21:46
-
-
Save kharmabum/6074872 to your computer and use it in GitHub Desktop.
HTML5 LocalStorage
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
| /** | |
| A simple key value store that uses LocalStorage | |
| @class KeyValueStore | |
| @namespace Discourse | |
| @module Discourse | |
| **/ | |
| Discourse.KeyValueStore = { | |
| initialized: false, | |
| context: "", | |
| init: function(ctx, messageBus) { | |
| this.initialized = true; | |
| this.context = ctx; | |
| }, | |
| abandonLocal: function() { | |
| var i, k; | |
| if (!(localStorage && this.initialized)) { | |
| return; | |
| } | |
| i = localStorage.length - 1; | |
| while (i >= 0) { | |
| k = localStorage.key(i); | |
| if (k.substring(0, this.context.length) === this.context) { | |
| localStorage.removeItem(k); | |
| } | |
| i--; | |
| } | |
| return true; | |
| }, | |
| remove: function(key) { | |
| return localStorage.removeItem(this.context + key); | |
| }, | |
| set: function(opts) { | |
| if (!(localStorage && this.initialized)) { | |
| return false; | |
| } | |
| localStorage[this.context + opts.key] = opts.value; | |
| }, | |
| get: function(key) { | |
| if (!localStorage) { | |
| return null; | |
| } | |
| return localStorage[this.context + key]; | |
| } | |
| }; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment