Improved load() and reset() assets/collections. Just refresh newer objects, ignore older or same objects.

This commit is contained in:
Martin Edenhofer 2014-11-10 20:44:38 +01:00
parent 6bc7dd9b2e
commit 4cbdbc79c0

View file

@ -67,17 +67,18 @@ class _collectionSingleton extends Spine.Module
@reset( localStorage: data.localStorage, type: type, data: collection ) @reset( localStorage: data.localStorage, type: type, data: collection )
reset: (params) -> reset: (params) ->
if !App[ params.type ]
# check if collection exists
appObject = App[ params.type ]
if !appObject
@log 'error', 'reset', "no such collection #{params.type}", params @log 'error', 'reset', "no such collection #{params.type}", params
return return
@log 'debug', 'reset', params
# remove permanent storage # remove permanent storage
@localDelete( params.type ) @localDelete( params.type )
# reset in-memory # reset in-memory
App[ params.type ].refresh( params.data, { clear: true } ) appObject.refresh( params.data, { clear: true } )
# remember in store if not already requested from local storage # remember in store if not already requested from local storage
for object in params.data for object in params.data
@ -89,19 +90,21 @@ class _collectionSingleton extends Spine.Module
@load( localStorage: false, type: type, data: collections ) @load( localStorage: false, type: type, data: collections )
load: (params) -> load: (params) ->
@log 'debug', 'load', params
# no data to load
return if _.isEmpty( params.data ) return if _.isEmpty( params.data )
if !App[ params.type ] # check if collection exists
@log 'error', 'reset', 'no such collection', params appObject = App[ params.type ]
if !appObject
@log 'error', 'reset', "no such collection #{params.type}", params
return return
localStorage = params.localStorage localStorage = params.localStorage
# load full array once # load full array once
if _.isArray( params.data ) if _.isArray( params.data )
App[ params.type ].refresh( params.data ) appObject.refresh( params.data )
# remember in store if not already requested from local storage # remember in store if not already requested from local storage
if !localStorage if !localStorage
@ -111,8 +114,17 @@ class _collectionSingleton extends Spine.Module
# load data from object # load data from object
for key, object of params.data for key, object of params.data
if !params.refresh && App[ params.type ] if !params.refresh && appObject
App[ params.type ].refresh( object )
# check if new object is newer, just load newer objects
if object.updated_at && appObject.exists( key )
exists = appObject.find( key )
if exists.updated_at && exists.updated_at isnt object.updated_at
appObject.refresh( object )
else
appObject.refresh( object )
else
appObject.refresh( object )
# remember in store if not already requested from local storage # remember in store if not already requested from local storage
if !localStorage if !localStorage