fredrik.eriksson

Coffee and a keyboard

Localstorage object storage

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

Leave a Response