-
-
Save MAMISHO/c4c9e82ac996b20a493473a077af0670 to your computer and use it in GitHub Desktop.
Storage - localStorage wrapper for enyo 2
This file contains 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
enyo.kind({ | |
name: "Storage", | |
kind: "Component", | |
statics: { | |
/* Set the value of item[key] to the stringified version of obj. */ | |
set: function(name, obj){ | |
localStorage.setItem(name, JSON.stringify(obj)); | |
}, | |
/* Get the item with the key 'name'. */ | |
get: function(name){ | |
var result; | |
if(typeof name === "string") { | |
result = localStorage.getItem(name); | |
} | |
if(typeof result === "string"){ | |
return JSON.parse(result); | |
} else if(typeof result === "object" && result !== null) { | |
enyo.log("OBJECT: " + result); | |
throw "ERROR [Storage.get]: getItem returned an object. Should be a string."; | |
} else if(typeof result === "undefined" || result === null){ | |
throw "ERROR: [Storage.get]: getItem returned a falsey value. Should be a string."; | |
} | |
}, | |
/* Remove the item with the key 'name'. */ | |
remove: function(name){ | |
if(typeof name === "string") { | |
localStorage.remove(name); | |
} else { | |
throw "ERROR [Storage.remove]: 'name' was not a String."; | |
} | |
}, | |
/* Returns length of all localStorage objects. */ | |
__getSize: function(){ | |
var i, count = 0; | |
for(i = 0; i < localStorage.length; i++){ | |
count += localStorage.getItem(localStorage.key()).length; | |
} | |
return count; | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment