Localstorage object storage
Posted November 27th, 2011 . No Comments .
Don’t remember where I got this snippet of code but it’s pretty useful so thanks to however wrote it
.
This script let’s you persist any javascript object in the localstorage with a key and a json string.
if window.Storage and window.JSON window.$storage = (key) -> set: (value) -> localStorage.setItem(key, JSON.stringify(value)) get: -> item = localStorage.getItem(key) JSON.parse(item) if item
And it’s used as follows:
$storage('test').set({"hello": "world"}) alert $storage('test').get().hello
No Responses to “Localstorage object storage”
RSS feed for comments on this post. TrackBack URL
