Created
April 28, 2012 17:43
-
-
Save angus-c/2520731 to your computer and use it in GitHub Desktop.
storage with functional mixins
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
| function withStorage() { | |
| if (window.localStorage) { | |
| localStorage.call(this); | |
| } else if (document.documentElement.addBehavior) { | |
| userData.call(this); | |
| } else { | |
| memory.call(this); | |
| } | |
| this.encode = function(val) { | |
| return JSON.stringify(val); | |
| }; | |
| this.decode = function(val) { | |
| return JSON.parse(val); | |
| }; | |
| /****************html5 local storage***************/ | |
| function localStorage() { | |
| this.initialize = function() {//... | |
| this.getItem = function(key) { | |
| return this.decode(window.localStorage.getItem(this.prefix + key)); | |
| } | |
| this.setItem = function(key, val) { | |
| return window.localStorage.setItem(this.prefix + key, this.encode(val)); | |
| } | |
| this.removeItem = function(key) { | |
| return window.localStorage.removeItem(this.prefix + key); | |
| } | |
| this.keys = function() {//... | |
| this.clear = function() {//... | |
| this.clearAll = function() {//... | |
| } | |
| /****************earlier IE ersatz storage***************/ | |
| function userData() { | |
| this.initialize = function(namespace) { | |
| if (//...) { | |
| this.createStorageElement(); | |
| } | |
| this.xmlDoc = this.dataStore.xmlDocument; | |
| this.xmlDocEl = this.xmlDoc.documentElement; | |
| //... | |
| } | |
| this.createStorageElement = function() { | |
| this.dataStore = document.createElement('div'); | |
| //... | |
| } | |
| this.getItem = function(key) { | |
| return this.decode(//...); | |
| } | |
| this.setItem = function(key, val) { | |
| return this.encode(//...); | |
| } | |
| this.keys = function() {//... | |
| this.clear = function() {//... | |
| this.clearAll = function() {//... | |
| } | |
| /****************temporary hash storage only***************/ | |
| function memory() { | |
| this.initialize = function(namespace) { | |
| //.. | |
| if (!this.memoryStore) { | |
| this.memoryStore = {}; | |
| } | |
| //.. | |
| } | |
| this.getItem = function(key) { | |
| return this.decode(this.memoryStore[key]); | |
| } | |
| this.setItem = function(key, val) { | |
| return this.memoryStore[key] = this.encode(val); | |
| } | |
| this.keys = function() {//... | |
| this.clear = function() {//... | |
| this.clearAll = function() {//... | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment