Moved to own App.LocalStorage and App.SessionStorage layer to abstract JSON.stringify and JSON.parse.

This commit is contained in:
Martin Edenhofer 2015-11-03 18:24:45 +01:00
parent 2cf47e24a9
commit 0143eae963
12 changed files with 144 additions and 21 deletions

View file

@ -443,7 +443,7 @@ class App.Controller extends Spine.Controller
}
processData: true,
success: (data, status, xhr) ->
App.Store.write( "user-ticket-popover::#{params.user_id}", data )
App.SessionStorage.set( "user-ticket-popover::#{params.user_id}", data )
# load assets
App.Collection.loadAssets( data.assets )
@ -452,7 +452,7 @@ class App.Controller extends Spine.Controller
)
# get data
data = App.Store.get( "user-ticket-popover::#{params.user_id}" )
data = App.SessionStorage.get( "user-ticket-popover::#{params.user_id}" )
if data
show( params, { open: data.ticket_ids_open, closed: data.ticket_ids_closed } )
@delay(

View file

@ -500,13 +500,13 @@ class App.ControllerTable extends App.Controller
data[type][key] = {}
data[type][key] = value
@log 'debug', @table_id, 'preferencesStore', data
localStorage.setItem(@preferencesStoreKey(), JSON.stringify(data))
App.LocalStorage.set(@preferencesStoreKey(), data)
preferencesGet: =>
data = localStorage.getItem(@preferencesStoreKey())
data = App.LocalStorage.get(@preferencesStoreKey())
return {} if !data
@log 'debug', @table_id, 'preferencesGet', data
JSON.parse(data)
data
preferencesStoreKey: =>
"tablePreferences:#{@table_id}"

View file

@ -10,7 +10,7 @@ class App.DashboardActivityStream extends App.Controller
fetch: =>
# use cache of first page
cache = App.Store.get( 'activity_stream' )
cache = App.LocalStorage.get( 'activity_stream' )
if cache
@load( cache )
@ -25,7 +25,7 @@ class App.DashboardActivityStream extends App.Controller
}
processData: true
success: (data) =>
App.Store.write( 'activity_stream', data )
App.LocalStorage.set( 'activity_stream', data )
@load(data)
)

View file

@ -11,7 +11,7 @@ class App.DashboardRss extends App.Controller
fetch: =>
# get data from cache
cache = App.Store.get( 'dashboard_rss' )
cache = App.LocalStorage.get( 'dashboard_rss' )
if cache
cache.head = 'Heise ATOM'
@render( cache )
@ -34,7 +34,7 @@ class App.DashboardRss extends App.Controller
message: data.message
)
else
App.Store.write( 'dashboard_rss', data )
App.LocalStorage.set( 'dashboard_rss', data )
data.head = 'Heise ATOM'
@render(data)
error: =>

View file

@ -134,7 +134,7 @@ class App.TicketCreate extends App.Controller
fetch: (params) ->
# use cache
cache = App.Store.get( 'ticket_create_attributes' )
cache = App.SessionStorage.get( 'ticket_create_attributes' )
if cache && !params.ticket_id && !params.article_id
@ -157,7 +157,7 @@ class App.TicketCreate extends App.Controller
success: (data, status, xhr) =>
# cache request
App.Store.write( 'ticket_create_attributes', data )
App.SessionStorage.set( 'ticket_create_attributes', data )
# get edit form attributes
@form_meta = data.form_meta

View file

@ -22,7 +22,7 @@ class Index extends App.ControllerContent
fetch: (params) ->
# use cache
cache = App.Store.get( 'ticket_create_attributes' )
cache = App.SessionStorage.get( 'ticket_create_attributes' )
if cache
@ -42,7 +42,7 @@ class Index extends App.ControllerContent
success: (data, status, xhr) =>
# cache request
App.Store.write( 'ticket_create_attributes', data )
App.SessionStorage.set( 'ticket_create_attributes', data )
# get edit form attributes
@form_meta = data.form_meta

View file

@ -23,9 +23,9 @@ class Index extends App.ControllerContent
return @params if @params
@params = {}
paramsRaw = localStorage.getItem('report::params')
paramsRaw = App.SessionStorage.get('report::params')
if paramsRaw
@params = JSON.parse(paramsRaw)
@params = paramsRaw
return @params
@params.timeRange = 'year'
@ -58,7 +58,7 @@ class Index extends App.ControllerContent
storeParams: =>
# store latest params
localStorage.setItem('report::params', JSON.stringify(@params))
App.SessionStorage.set('report::params', @params)
render: (data = {}) =>

View file

@ -32,7 +32,7 @@ class App.TicketZoom extends App.Controller
@overview_id = false
@key = 'ticket::' + @ticket_id
cache = App.Store.get(@key)
cache = App.LocalStorage.get(@key)
if cache
@load(cache)
update = =>
@ -168,7 +168,7 @@ class App.TicketZoom extends App.Controller
@ticketUpdatedAtLastCall = newTicketRaw.updated_at
@load(data, force)
App.Store.write(@key, data)
App.LocalStorage(@key, data)
if !@doNotLog
@doNotLog = 1

View file

@ -91,7 +91,7 @@ class App.TicketZoomHighlighter extends App.Controller
articles.off('mousedown', @onMouseDown)
articles.on('mousedown', @onMouseDown) #future: touchend
# for testing purposes the highlights get stored in localStorage
# for testing purposes the highlights get stored in atrticle preferences
loadHighlights: (ticket_article_id) ->
return if !@isRole('Agent')
article = App.TicketArticle.find(ticket_article_id)

View file

@ -19,7 +19,7 @@ class App.WidgetTag extends App.Controller
@render()
return
@tags = App.Store.get( @cacheKey ) || []
@tags = App.LocalStorage.get( @cacheKey ) || []
if !_.isEmpty(@tags)
@render()
@delay(
@ -42,7 +42,7 @@ class App.WidgetTag extends App.Controller
processData: true
success: (data, status, xhr) =>
@tags = data.tags
App.Store.write( @cacheKey, @tags )
App.LocalStorage.set( @cacheKey, @tags )
@render()
)

View file

@ -0,0 +1,60 @@
class App.LocalStorage
_instance = undefined # Must be declared here to force the closure on the class
@set: (key, value) ->
if _instance == undefined
_instance ?= new _storeSingleton
_instance.set(key, value)
@get: (args) ->
if _instance == undefined
_instance ?= new _storeSingleton
_instance.get(args)
@delete: (args) ->
if _instance == undefined
_instance ?= new _storeSingleton
_instance.delete(args)
@clear: ->
if _instance == undefined
_instance ?= new _storeSingleton
_instance.clear()
@list: ->
if _instance == undefined
_instance ?= new _storeSingleton
_instance.list()
# The actual Singleton class
class _storeSingleton
constructor: ->
# write to local storage
set: (key, value) ->
try
localStorage.setItem(key, JSON.stringify( value ))
catch e
if e is QUOTA_EXCEEDED_ERR
# do something nice to notify your users
App.Log.error 'App.LocalStore', 'Local storage quote exceeded!'
# get item
get: (key) ->
value = localStorage.getItem(key)
return if !value
JSON.parse(value)
# delete item
delete: (key) ->
localStorage.removeItem(key)
# clear local storage
clear: ->
localStorage.clear()
# return list of all keys
list: ->
for key of window.localStorage
list.push key
list

View file

@ -0,0 +1,63 @@
class App.SessionStorage
_instance = undefined # Must be declared here to force the closure on the class
@set: (key, value) ->
if _instance == undefined
_instance ?= new _storeSingleton
_instance.set(key, value)
@get: (args) ->
if _instance == undefined
_instance ?= new _storeSingleton
_instance.get(args)
@delete: (args) ->
if _instance == undefined
_instance ?= new _storeSingleton
_instance.delete(args)
@clear: ->
if _instance == undefined
_instance ?= new _storeSingleton
_instance.clear()
@list: ->
if _instance == undefined
_instance ?= new _storeSingleton
_instance.list()
# The actual Singleton class
class _storeSingleton
constructor: ->
App.Event.bind 'clearStore', =>
@clear()
# write to local storage
set: (key, value) ->
try
sessionStorage.setItem(key, JSON.stringify( value ))
catch e
if e is QUOTA_EXCEEDED_ERR
# do something nice to notify your users
App.Log.error 'App.LocalStore', 'Local storage quote exceeded!'
# get item
get: (key) ->
value = sessionStorage.getItem(key)
return if !value
JSON.parse(value)
# delete item
delete: (key) ->
sessionStorage.removeItem(key)
# clear local storage
clear: ->
sessionStorage.clear()
# return list of all keys
list: ->
for key of window.sessionStorage
list.push key
list