Some improvements for local storage on IE.

This commit is contained in:
Martin Edenhofer 2012-11-27 03:07:59 +01:00
parent a07b137b8a
commit a24c632cbf

View file

@ -30,13 +30,27 @@ class App.Store
# The actual Singleton class # The actual Singleton class
class _Singleton class _Singleton
store: {}
constructor: ->
@support = true
if !window.localStorage
@support = false
# @support = false
# write to local storage # write to local storage
write: (key, value) -> write: (key, value) ->
localStorage.setItem( key, JSON.stringify( value ) ) @store[key] = value
return if !@support
try
localStorage.setItem( key, JSON.stringify( value ) )
catch e
if e is QUOTA_EXCEEDED_ERR
# do something nice to notify your users
App.Log.log 'App.Store', 'error', 'Local storage quote exceeded, please relogin!'
# get item # get item
get: (key) -> get: (key) ->
return @store[key] if !@support
value = localStorage.getItem( key ) value = localStorage.getItem( key )
return if !value return if !value
object = JSON.parse( value ) object = JSON.parse( value )
@ -44,18 +58,28 @@ class _Singleton
# delete item # delete item
delete: (key) -> delete: (key) ->
@store.delete key
return if !@support
localStorage.removeItem( key ) localStorage.removeItem( key )
# clear local storage # clear local storage
clear: -> clear: ->
@store = {}
localStorage.clear() localStorage.clear()
# return list of all keys # return list of all keys
list: -> list: ->
list = [] list = []
logLength = localStorage.length-1; if !@support
for count in [0..logLength] for key of @store
key = localStorage.key( count )
if key
list.push key list.push key
return list
# logLength = localStorage.length-1;
# for count in [0..logLength]
# key = localStorage.key( count )
# if key
# list.push key
for key of window.localStorage
list.push key
list list