Removed storage options from collection helper.
This commit is contained in:
parent
0143eae963
commit
4a9c2c55b2
3 changed files with 24 additions and 168 deletions
|
@ -4,25 +4,25 @@ class App.Collection
|
||||||
@init: ->
|
@init: ->
|
||||||
_instance = new _collectionSingleton
|
_instance = new _collectionSingleton
|
||||||
|
|
||||||
@load: ( args ) ->
|
@load: (args) ->
|
||||||
if _instance == undefined
|
if _instance == undefined
|
||||||
_instance ?= new _collectionSingleton
|
_instance ?= new _collectionSingleton
|
||||||
_instance.load( args )
|
_instance.load(args)
|
||||||
|
|
||||||
@loadAssets: ( args ) ->
|
@loadAssets: (args) ->
|
||||||
if _instance == undefined
|
if _instance == undefined
|
||||||
_instance ?= new _collectionSingleton
|
_instance ?= new _collectionSingleton
|
||||||
_instance.loadAssets( args )
|
_instance.loadAssets(args)
|
||||||
|
|
||||||
@reset: ( args ) ->
|
@reset: (args) ->
|
||||||
if _instance == undefined
|
if _instance == undefined
|
||||||
_instance ?= new _collectionSingleton
|
_instance ?= new _collectionSingleton
|
||||||
_instance.reset( args )
|
_instance.reset(args)
|
||||||
|
|
||||||
@resetCollections: ( args ) ->
|
@resetCollections: (args) ->
|
||||||
if _instance == undefined
|
if _instance == undefined
|
||||||
_instance ?= new _collectionSingleton
|
_instance ?= new _collectionSingleton
|
||||||
_instance.resetCollections( args )
|
_instance.resetCollections(args)
|
||||||
|
|
||||||
class _collectionSingleton extends Spine.Module
|
class _collectionSingleton extends Spine.Module
|
||||||
@include App.LogInclude
|
@include App.LogInclude
|
||||||
|
@ -35,7 +35,7 @@ class _collectionSingleton extends Spine.Module
|
||||||
@log 'error', 'loadAssets:trigger, got no data, cant load assets'
|
@log 'error', 'loadAssets:trigger, got no data, cant load assets'
|
||||||
return
|
return
|
||||||
|
|
||||||
@loadAssets( data )
|
@loadAssets(data)
|
||||||
|
|
||||||
# add trigger - bind new events
|
# add trigger - bind new events
|
||||||
App.Event.bind 'resetCollection', (data) =>
|
App.Event.bind 'resetCollection', (data) =>
|
||||||
|
@ -43,28 +43,14 @@ class _collectionSingleton extends Spine.Module
|
||||||
@log 'error', 'resetCollection:trigger, got no data, cant for collections'
|
@log 'error', 'resetCollection:trigger, got no data, cant for collections'
|
||||||
return
|
return
|
||||||
|
|
||||||
@resetCollections( data )
|
@resetCollections(data)
|
||||||
|
|
||||||
# find collections to load
|
|
||||||
@_loadObjectsFromSessionStore()
|
|
||||||
|
|
||||||
_loadObjectsFromSessionStore: ->
|
|
||||||
list = App.Store.list()
|
|
||||||
for key in list
|
|
||||||
parts = key.split('::')
|
|
||||||
if parts[0] is 'collection'
|
|
||||||
data = App.Store.get( key )
|
|
||||||
data['type'] = parts[1]
|
|
||||||
data['sessionStorage'] = true
|
|
||||||
|
|
||||||
@log 'debug', 'load INIT', data
|
|
||||||
@load( data )
|
|
||||||
|
|
||||||
resetCollections: (data) ->
|
resetCollections: (data) ->
|
||||||
# load assets
|
|
||||||
|
# load collection
|
||||||
for type, collection of data
|
for type, collection of data
|
||||||
@log 'debug', 'resetCollection:trigger', type, collection
|
@log 'debug', 'resetCollection:trigger', type, collection
|
||||||
@reset( sessionStorage: data.sessionStorage, type: type, data: collection )
|
@reset(type: type, data: collection)
|
||||||
|
|
||||||
reset: (params) ->
|
reset: (params) ->
|
||||||
|
|
||||||
|
@ -74,42 +60,28 @@ class _collectionSingleton extends Spine.Module
|
||||||
@log 'error', 'reset', "no such collection #{params.type}", params
|
@log 'error', 'reset', "no such collection #{params.type}", params
|
||||||
return
|
return
|
||||||
|
|
||||||
# remove permanent storage
|
|
||||||
@localDelete( params.type )
|
|
||||||
|
|
||||||
# reset in-memory
|
# reset in-memory
|
||||||
appObject.refresh( params.data, { clear: true } )
|
appObject.refresh(params.data, { clear: true })
|
||||||
|
|
||||||
# remember in store if not already requested from local storage
|
|
||||||
for object in params.data
|
|
||||||
@localStore( params.type, object )
|
|
||||||
|
|
||||||
loadAssets: (assets) ->
|
loadAssets: (assets) ->
|
||||||
@log 'debug', 'loadAssets', assets
|
@log 'debug', 'loadAssets', assets
|
||||||
for type, collections of assets
|
for type, collections of assets
|
||||||
@load( sessionStorage: false, type: type, data: collections )
|
@load(type: type, data: collections)
|
||||||
|
|
||||||
load: (params) ->
|
load: (params) ->
|
||||||
|
|
||||||
# no data to load
|
# no data to load
|
||||||
return if _.isEmpty( params.data )
|
return if _.isEmpty(params.data)
|
||||||
|
|
||||||
# check if collection exists
|
# check if collection exists
|
||||||
appObject = App[ params.type ]
|
appObject = App[params.type]
|
||||||
if !appObject
|
if !appObject
|
||||||
@log 'error', 'reset', "no such collection #{params.type}", params
|
@log 'error', 'reset', "no such collection #{params.type}", params
|
||||||
return
|
return
|
||||||
|
|
||||||
sessionStorage = params.sessionStorage
|
|
||||||
|
|
||||||
# load full array once
|
# load full array once
|
||||||
if _.isArray( params.data )
|
if _.isArray(params.data)
|
||||||
appObject.refresh( params.data )
|
appObject.refresh(params.data)
|
||||||
|
|
||||||
# remember in store if not already requested from local storage
|
|
||||||
if !sessionStorage
|
|
||||||
for object in params.data
|
|
||||||
@localStore( params.type, object )
|
|
||||||
return
|
return
|
||||||
|
|
||||||
# load data from object
|
# load data from object
|
||||||
|
@ -117,26 +89,12 @@ class _collectionSingleton extends Spine.Module
|
||||||
if !params.refresh && appObject
|
if !params.refresh && appObject
|
||||||
|
|
||||||
# check if new object is newer, just load newer objects
|
# check if new object is newer, just load newer objects
|
||||||
if object.updated_at && appObject.exists( key )
|
if object.updated_at && appObject.exists(key)
|
||||||
exists = appObject.find( key )
|
exists = appObject.find(key)
|
||||||
if exists.updated_at
|
if exists.updated_at
|
||||||
if exists.updated_at < object.updated_at
|
if exists.updated_at < object.updated_at
|
||||||
appObject.refresh( object )
|
appObject.refresh(object)
|
||||||
else
|
else
|
||||||
appObject.refresh( object )
|
appObject.refresh(object)
|
||||||
else
|
else
|
||||||
appObject.refresh( object )
|
appObject.refresh(object)
|
||||||
|
|
||||||
# remember in store if not already requested from local storage
|
|
||||||
if !sessionStorage
|
|
||||||
@localStore( params.type, object)
|
|
||||||
|
|
||||||
localDelete: (type) ->
|
|
||||||
list = App.Store.list()
|
|
||||||
for key in list
|
|
||||||
parts = key.split('::')
|
|
||||||
if parts[0] is 'collection' && parts[1] is type
|
|
||||||
App.Store.delete(key)
|
|
||||||
|
|
||||||
localStore: (type, object) ->
|
|
||||||
App.Store.write( 'collection::' + type + '::' + object.id, { data: [ object ] } )
|
|
||||||
|
|
|
@ -1,93 +0,0 @@
|
||||||
class App.Store
|
|
||||||
_instance = undefined # Must be declared here to force the closure on the class
|
|
||||||
@renew: ->
|
|
||||||
_instance = new _storeSingleton
|
|
||||||
|
|
||||||
@write: (key, value) ->
|
|
||||||
if _instance == undefined
|
|
||||||
_instance ?= new _storeSingleton
|
|
||||||
_instance.write(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
|
|
||||||
store: {}
|
|
||||||
constructor: ->
|
|
||||||
@support = true
|
|
||||||
if !window.sessionStorage
|
|
||||||
@support = false
|
|
||||||
# @support = false
|
|
||||||
|
|
||||||
# clear store on every login/logout
|
|
||||||
if @support
|
|
||||||
App.Event.bind 'clearStore', =>
|
|
||||||
@clear('all')
|
|
||||||
|
|
||||||
# write to local storage
|
|
||||||
write: (key, value) ->
|
|
||||||
@store[key] = value
|
|
||||||
return if !@support
|
|
||||||
return if !App.Config.get('ui_client_storage')
|
|
||||||
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.Store', 'Local storage quote exceeded, please relogin!'
|
|
||||||
|
|
||||||
# get item
|
|
||||||
get: (key) ->
|
|
||||||
return @store[key] if !@support
|
|
||||||
return @store[key] if !App.Config.get('ui_client_storage')
|
|
||||||
value = sessionStorage.getItem( key )
|
|
||||||
return if !value
|
|
||||||
object = JSON.parse( value )
|
|
||||||
return object
|
|
||||||
|
|
||||||
# delete item
|
|
||||||
delete: (key) ->
|
|
||||||
delete @store[key]
|
|
||||||
return if !@support
|
|
||||||
return if !App.Config.get('ui_client_storage')
|
|
||||||
sessionStorage.removeItem( key )
|
|
||||||
|
|
||||||
# clear local storage
|
|
||||||
clear: ->
|
|
||||||
@store = {}
|
|
||||||
sessionStorage.clear()
|
|
||||||
|
|
||||||
# return list of all keys
|
|
||||||
list: ->
|
|
||||||
list = []
|
|
||||||
if !@support || !App.Config.get('ui_client_storage')
|
|
||||||
for key of @store
|
|
||||||
list.push key
|
|
||||||
return list
|
|
||||||
|
|
||||||
# logLength = sessionStorage.length-1;
|
|
||||||
# for count in [0..logLength]
|
|
||||||
# key = sessionStorage.key( count )
|
|
||||||
# if key
|
|
||||||
# list.push key
|
|
||||||
for key of window.sessionStorage
|
|
||||||
list.push key
|
|
||||||
list
|
|
|
@ -1,18 +1,9 @@
|
||||||
class App.Model extends Spine.Model
|
class App.Model extends Spine.Model
|
||||||
@destroyBind: false
|
|
||||||
@apiPath: App.Config.get('api_path')
|
@apiPath: App.Config.get('api_path')
|
||||||
|
|
||||||
constructor: ->
|
constructor: ->
|
||||||
super
|
super
|
||||||
|
|
||||||
# delete object from local storage on destroy
|
|
||||||
if !@constructor.destroyBind
|
|
||||||
@bind( 'destroy', (e) ->
|
|
||||||
className = Object.getPrototypeOf(e).constructor.className
|
|
||||||
key = "collection::#{className}::#{e.id}"
|
|
||||||
App.Store.delete(key)
|
|
||||||
)
|
|
||||||
|
|
||||||
uiUrl: ->
|
uiUrl: ->
|
||||||
'#'
|
'#'
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue