Moved to own App.LocalStorage and App.SessionStorage layer to abstract JSON.stringify and JSON.parse.
This commit is contained in:
parent
2cf47e24a9
commit
0143eae963
12 changed files with 144 additions and 21 deletions
|
@ -443,7 +443,7 @@ class App.Controller extends Spine.Controller
|
||||||
}
|
}
|
||||||
processData: true,
|
processData: true,
|
||||||
success: (data, status, xhr) ->
|
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
|
# load assets
|
||||||
App.Collection.loadAssets( data.assets )
|
App.Collection.loadAssets( data.assets )
|
||||||
|
@ -452,7 +452,7 @@ class App.Controller extends Spine.Controller
|
||||||
)
|
)
|
||||||
|
|
||||||
# get data
|
# get data
|
||||||
data = App.Store.get( "user-ticket-popover::#{params.user_id}" )
|
data = App.SessionStorage.get( "user-ticket-popover::#{params.user_id}" )
|
||||||
if data
|
if data
|
||||||
show( params, { open: data.ticket_ids_open, closed: data.ticket_ids_closed } )
|
show( params, { open: data.ticket_ids_open, closed: data.ticket_ids_closed } )
|
||||||
@delay(
|
@delay(
|
||||||
|
|
|
@ -500,13 +500,13 @@ class App.ControllerTable extends App.Controller
|
||||||
data[type][key] = {}
|
data[type][key] = {}
|
||||||
data[type][key] = value
|
data[type][key] = value
|
||||||
@log 'debug', @table_id, 'preferencesStore', data
|
@log 'debug', @table_id, 'preferencesStore', data
|
||||||
localStorage.setItem(@preferencesStoreKey(), JSON.stringify(data))
|
App.LocalStorage.set(@preferencesStoreKey(), data)
|
||||||
|
|
||||||
preferencesGet: =>
|
preferencesGet: =>
|
||||||
data = localStorage.getItem(@preferencesStoreKey())
|
data = App.LocalStorage.get(@preferencesStoreKey())
|
||||||
return {} if !data
|
return {} if !data
|
||||||
@log 'debug', @table_id, 'preferencesGet', data
|
@log 'debug', @table_id, 'preferencesGet', data
|
||||||
JSON.parse(data)
|
data
|
||||||
|
|
||||||
preferencesStoreKey: =>
|
preferencesStoreKey: =>
|
||||||
"tablePreferences:#{@table_id}"
|
"tablePreferences:#{@table_id}"
|
||||||
|
|
|
@ -10,7 +10,7 @@ class App.DashboardActivityStream extends App.Controller
|
||||||
fetch: =>
|
fetch: =>
|
||||||
|
|
||||||
# use cache of first page
|
# use cache of first page
|
||||||
cache = App.Store.get( 'activity_stream' )
|
cache = App.LocalStorage.get( 'activity_stream' )
|
||||||
if cache
|
if cache
|
||||||
@load( cache )
|
@load( cache )
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ class App.DashboardActivityStream extends App.Controller
|
||||||
}
|
}
|
||||||
processData: true
|
processData: true
|
||||||
success: (data) =>
|
success: (data) =>
|
||||||
App.Store.write( 'activity_stream', data )
|
App.LocalStorage.set( 'activity_stream', data )
|
||||||
@load(data)
|
@load(data)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ class App.DashboardRss extends App.Controller
|
||||||
fetch: =>
|
fetch: =>
|
||||||
|
|
||||||
# get data from cache
|
# get data from cache
|
||||||
cache = App.Store.get( 'dashboard_rss' )
|
cache = App.LocalStorage.get( 'dashboard_rss' )
|
||||||
if cache
|
if cache
|
||||||
cache.head = 'Heise ATOM'
|
cache.head = 'Heise ATOM'
|
||||||
@render( cache )
|
@render( cache )
|
||||||
|
@ -34,7 +34,7 @@ class App.DashboardRss extends App.Controller
|
||||||
message: data.message
|
message: data.message
|
||||||
)
|
)
|
||||||
else
|
else
|
||||||
App.Store.write( 'dashboard_rss', data )
|
App.LocalStorage.set( 'dashboard_rss', data )
|
||||||
data.head = 'Heise ATOM'
|
data.head = 'Heise ATOM'
|
||||||
@render(data)
|
@render(data)
|
||||||
error: =>
|
error: =>
|
||||||
|
|
|
@ -134,7 +134,7 @@ class App.TicketCreate extends App.Controller
|
||||||
fetch: (params) ->
|
fetch: (params) ->
|
||||||
|
|
||||||
# use cache
|
# use cache
|
||||||
cache = App.Store.get( 'ticket_create_attributes' )
|
cache = App.SessionStorage.get( 'ticket_create_attributes' )
|
||||||
|
|
||||||
if cache && !params.ticket_id && !params.article_id
|
if cache && !params.ticket_id && !params.article_id
|
||||||
|
|
||||||
|
@ -157,7 +157,7 @@ class App.TicketCreate extends App.Controller
|
||||||
success: (data, status, xhr) =>
|
success: (data, status, xhr) =>
|
||||||
|
|
||||||
# cache request
|
# cache request
|
||||||
App.Store.write( 'ticket_create_attributes', data )
|
App.SessionStorage.set( 'ticket_create_attributes', data )
|
||||||
|
|
||||||
# get edit form attributes
|
# get edit form attributes
|
||||||
@form_meta = data.form_meta
|
@form_meta = data.form_meta
|
||||||
|
|
|
@ -22,7 +22,7 @@ class Index extends App.ControllerContent
|
||||||
fetch: (params) ->
|
fetch: (params) ->
|
||||||
|
|
||||||
# use cache
|
# use cache
|
||||||
cache = App.Store.get( 'ticket_create_attributes' )
|
cache = App.SessionStorage.get( 'ticket_create_attributes' )
|
||||||
|
|
||||||
if cache
|
if cache
|
||||||
|
|
||||||
|
@ -42,7 +42,7 @@ class Index extends App.ControllerContent
|
||||||
success: (data, status, xhr) =>
|
success: (data, status, xhr) =>
|
||||||
|
|
||||||
# cache request
|
# cache request
|
||||||
App.Store.write( 'ticket_create_attributes', data )
|
App.SessionStorage.set( 'ticket_create_attributes', data )
|
||||||
|
|
||||||
# get edit form attributes
|
# get edit form attributes
|
||||||
@form_meta = data.form_meta
|
@form_meta = data.form_meta
|
||||||
|
|
|
@ -23,9 +23,9 @@ class Index extends App.ControllerContent
|
||||||
return @params if @params
|
return @params if @params
|
||||||
|
|
||||||
@params = {}
|
@params = {}
|
||||||
paramsRaw = localStorage.getItem('report::params')
|
paramsRaw = App.SessionStorage.get('report::params')
|
||||||
if paramsRaw
|
if paramsRaw
|
||||||
@params = JSON.parse(paramsRaw)
|
@params = paramsRaw
|
||||||
return @params
|
return @params
|
||||||
|
|
||||||
@params.timeRange = 'year'
|
@params.timeRange = 'year'
|
||||||
|
@ -58,7 +58,7 @@ class Index extends App.ControllerContent
|
||||||
|
|
||||||
storeParams: =>
|
storeParams: =>
|
||||||
# store latest params
|
# store latest params
|
||||||
localStorage.setItem('report::params', JSON.stringify(@params))
|
App.SessionStorage.set('report::params', @params)
|
||||||
|
|
||||||
render: (data = {}) =>
|
render: (data = {}) =>
|
||||||
|
|
||||||
|
|
|
@ -32,7 +32,7 @@ class App.TicketZoom extends App.Controller
|
||||||
@overview_id = false
|
@overview_id = false
|
||||||
|
|
||||||
@key = 'ticket::' + @ticket_id
|
@key = 'ticket::' + @ticket_id
|
||||||
cache = App.Store.get(@key)
|
cache = App.LocalStorage.get(@key)
|
||||||
if cache
|
if cache
|
||||||
@load(cache)
|
@load(cache)
|
||||||
update = =>
|
update = =>
|
||||||
|
@ -168,7 +168,7 @@ class App.TicketZoom extends App.Controller
|
||||||
@ticketUpdatedAtLastCall = newTicketRaw.updated_at
|
@ticketUpdatedAtLastCall = newTicketRaw.updated_at
|
||||||
|
|
||||||
@load(data, force)
|
@load(data, force)
|
||||||
App.Store.write(@key, data)
|
App.LocalStorage(@key, data)
|
||||||
|
|
||||||
if !@doNotLog
|
if !@doNotLog
|
||||||
@doNotLog = 1
|
@doNotLog = 1
|
||||||
|
|
|
@ -91,7 +91,7 @@ class App.TicketZoomHighlighter extends App.Controller
|
||||||
articles.off('mousedown', @onMouseDown)
|
articles.off('mousedown', @onMouseDown)
|
||||||
articles.on('mousedown', @onMouseDown) #future: touchend
|
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) ->
|
loadHighlights: (ticket_article_id) ->
|
||||||
return if !@isRole('Agent')
|
return if !@isRole('Agent')
|
||||||
article = App.TicketArticle.find(ticket_article_id)
|
article = App.TicketArticle.find(ticket_article_id)
|
||||||
|
|
|
@ -19,7 +19,7 @@ class App.WidgetTag extends App.Controller
|
||||||
@render()
|
@render()
|
||||||
return
|
return
|
||||||
|
|
||||||
@tags = App.Store.get( @cacheKey ) || []
|
@tags = App.LocalStorage.get( @cacheKey ) || []
|
||||||
if !_.isEmpty(@tags)
|
if !_.isEmpty(@tags)
|
||||||
@render()
|
@render()
|
||||||
@delay(
|
@delay(
|
||||||
|
@ -42,7 +42,7 @@ class App.WidgetTag extends App.Controller
|
||||||
processData: true
|
processData: true
|
||||||
success: (data, status, xhr) =>
|
success: (data, status, xhr) =>
|
||||||
@tags = data.tags
|
@tags = data.tags
|
||||||
App.Store.write( @cacheKey, @tags )
|
App.LocalStorage.set( @cacheKey, @tags )
|
||||||
@render()
|
@render()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
60
app/assets/javascripts/app/lib/app_post/local_storage.coffee
Normal file
60
app/assets/javascripts/app/lib/app_post/local_storage.coffee
Normal 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
|
|
@ -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
|
Loading…
Reference in a new issue