Improved fetching of records (race conditions).
This commit is contained in:
parent
c27c6e7853
commit
3fefd7918e
1 changed files with 41 additions and 31 deletions
|
@ -106,25 +106,41 @@ class App.Model extends Spine.Model
|
||||||
if callback
|
if callback
|
||||||
callback( data )
|
callback( data )
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
if force
|
||||||
|
console.log 'debug', 'find forced to load!', @className, id
|
||||||
else
|
else
|
||||||
if force
|
console.log 'debug', 'find not loaded, load now!', @className, id
|
||||||
console.log 'debug', 'find forced to load!', @className, id
|
if callback
|
||||||
else
|
|
||||||
console.log 'debug', 'find not loaded!', @className, id
|
|
||||||
if callback
|
|
||||||
|
|
||||||
# execute callback if record got loaded
|
# store callback and requested id
|
||||||
App[ @className ].one 'refresh', (records) ->
|
if !@RETRIEVE_CALLBACK
|
||||||
|
@RETRIEVE_CALLBACK = {}
|
||||||
|
if !@RETRIEVE_CALLBACK[id]
|
||||||
|
@RETRIEVE_CALLBACK[id] = {}
|
||||||
|
key = @className + '-' + Math.floor( Math.random() * 99999 )
|
||||||
|
@RETRIEVE_CALLBACK[id][key] = callback
|
||||||
|
|
||||||
|
# bind refresh event
|
||||||
|
if !@RETRIEVE_BIND
|
||||||
|
@RETRIEVE_BIND = true
|
||||||
|
|
||||||
|
# check if bind for requested id exists
|
||||||
|
App[ @className ].bind 'refresh', (records) ->
|
||||||
for record in records
|
for record in records
|
||||||
if record.id.toString() is id.toString()
|
if @RETRIEVE_CALLBACK[ record.id ]
|
||||||
data = App[ @className ].find( record.id )
|
for key, callback of @RETRIEVE_CALLBACK[ record.id ]
|
||||||
callback( data )
|
data = App[ @className ].find( record.id )
|
||||||
|
callback( data )
|
||||||
|
delete @RETRIEVE_CALLBACK[ record.id ][ key ]
|
||||||
|
if _.isEmpty @RETRIEVE_CALLBACK[ record.id ]
|
||||||
|
delete @RETRIEVE_CALLBACK[ record.id ]
|
||||||
|
|
||||||
# fetch object
|
# fetch object
|
||||||
console.log 'debug', 'loading..' + @className + '..', id
|
console.log 'debug', 'loading..' + @className + '..', id
|
||||||
App[ @className ].fetch( id: id )
|
App[ @className ].fetch( id: id )
|
||||||
return true
|
return true
|
||||||
return false
|
return false
|
||||||
|
|
||||||
@subscribe: (callback, param = {}) ->
|
@subscribe: (callback, param = {}) ->
|
||||||
if !@SUBSCRIPTION_COLLECTION
|
if !@SUBSCRIPTION_COLLECTION
|
||||||
|
@ -134,8 +150,8 @@ class App.Model extends Spine.Model
|
||||||
@bind(
|
@bind(
|
||||||
'refresh change'
|
'refresh change'
|
||||||
=>
|
=>
|
||||||
for key, callbackSingle of @SUBSCRIPTION_COLLECTION
|
for key, callback of @SUBSCRIPTION_COLLECTION
|
||||||
callbackSingle()
|
callback()
|
||||||
)
|
)
|
||||||
|
|
||||||
# trigger deleteAll() and fetch() on network notify
|
# trigger deleteAll() and fetch() on network notify
|
||||||
|
@ -144,17 +160,11 @@ class App.Model extends Spine.Model
|
||||||
events
|
events
|
||||||
=>
|
=>
|
||||||
@deleteAll()
|
@deleteAll()
|
||||||
# callbacks = =>
|
|
||||||
# for key, callbackSingle of @SUBSCRIPTION_COLLECTION
|
|
||||||
# callbackSingle()
|
|
||||||
# @one 'refresh', (collection) =>
|
|
||||||
# callbacks(collection)
|
|
||||||
@fetch()
|
@fetch()
|
||||||
|
|
||||||
'Collection::Subscribe::' + @className
|
'Collection::Subscribe::' + @className
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
key = @className + '-' + Math.floor( Math.random() * 99999 )
|
key = @className + '-' + Math.floor( Math.random() * 99999 )
|
||||||
@SUBSCRIPTION_COLLECTION[key] = callback
|
@SUBSCRIPTION_COLLECTION[key] = callback
|
||||||
|
|
||||||
|
@ -167,26 +177,26 @@ class App.Model extends Spine.Model
|
||||||
return key
|
return key
|
||||||
|
|
||||||
subscribe: (callback) ->
|
subscribe: (callback) ->
|
||||||
|
|
||||||
|
# init bind
|
||||||
if !App[ @constructor.className ]['SUBSCRIPTION_ITEM']
|
if !App[ @constructor.className ]['SUBSCRIPTION_ITEM']
|
||||||
App[ @constructor.className ]['SUBSCRIPTION_ITEM'] = {}
|
App[ @constructor.className ]['SUBSCRIPTION_ITEM'] = {}
|
||||||
if !App[ @constructor.className ]['SUBSCRIPTION_ITEM'][@id]
|
|
||||||
App[ @constructor.className ]['SUBSCRIPTION_ITEM'][@id] = {}
|
|
||||||
|
|
||||||
events = "#{@constructor.className}:created #{@constructor.className}:updated #{@constructor.className}:destroy"
|
events = "#{@constructor.className}:created #{@constructor.className}:updated #{@constructor.className}:destroy"
|
||||||
App.Event.bind(
|
App.Event.bind(
|
||||||
events
|
events
|
||||||
(record) =>
|
(record) =>
|
||||||
if @id.toString() is record.id.toString()
|
if @id.toString() is record.id.toString()
|
||||||
App[ @constructor.className ].one 'refresh', (record) =>
|
for key, callback of App[ @constructor.className ]['SUBSCRIPTION_ITEM'][ @id ]
|
||||||
user = App[ @constructor.className ].find(@id)
|
App[ @constructor.className ].retrieve( @id, callback, true )
|
||||||
for key, callback of App[ @constructor.className ]['SUBSCRIPTION_ITEM'][@id]
|
|
||||||
callback(user)
|
|
||||||
App[ @constructor.className ].fetch( id: @id )
|
|
||||||
'Item::Subscribe::' + @constructor.className
|
'Item::Subscribe::' + @constructor.className
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# remember record id and callback
|
||||||
|
if !App[ @constructor.className ]['SUBSCRIPTION_ITEM'][ @id ]
|
||||||
|
App[ @constructor.className ]['SUBSCRIPTION_ITEM'][ @id ] = {}
|
||||||
key = @constructor.className + '-' + Math.floor( Math.random() * 99999 )
|
key = @constructor.className + '-' + Math.floor( Math.random() * 99999 )
|
||||||
App[ @constructor.className ]['SUBSCRIPTION_ITEM'][@id][key] = callback
|
App[ @constructor.className ]['SUBSCRIPTION_ITEM'][ @id ][key] = callback
|
||||||
return key
|
return key
|
||||||
|
|
||||||
@unsubscribe: (data) ->
|
@unsubscribe: (data) ->
|
||||||
|
|
Loading…
Reference in a new issue