From a24c632cbf4e69aabdcf4169f065147c57a9c0c3 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 27 Nov 2012 03:07:59 +0100 Subject: [PATCH] Some improvements for local storage on IE. --- .../app/lib/app_post/store.js.coffee | 34 ++++++++++++++++--- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/app/assets/javascripts/app/lib/app_post/store.js.coffee b/app/assets/javascripts/app/lib/app_post/store.js.coffee index 3507d5ead..cbab3ac62 100644 --- a/app/assets/javascripts/app/lib/app_post/store.js.coffee +++ b/app/assets/javascripts/app/lib/app_post/store.js.coffee @@ -30,13 +30,27 @@ class App.Store # The actual Singleton class class _Singleton + store: {} + constructor: -> + @support = true + if !window.localStorage + @support = false +# @support = false # write to local storage 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: (key) -> + return @store[key] if !@support value = localStorage.getItem( key ) return if !value object = JSON.parse( value ) @@ -44,18 +58,28 @@ class _Singleton # delete item delete: (key) -> + @store.delete key + return if !@support localStorage.removeItem( key ) # clear local storage clear: -> + @store = {} localStorage.clear() # return list of all keys list: -> list = [] - logLength = localStorage.length-1; - for count in [0..logLength] - key = localStorage.key( count ) - if key + if !@support + for key of @store 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 \ No newline at end of file