Improved fetching of records (race conditions).

This commit is contained in:
Martin Edenhofer 2013-11-02 15:42:23 +01:00
parent c27c6e7853
commit 3fefd7918e

View file

@ -106,19 +106,35 @@ class App.Model extends Spine.Model
if callback if callback
callback( data ) callback( data )
return data return data
else
if force if force
console.log 'debug', 'find forced to load!', @className, id console.log 'debug', 'find forced to load!', @className, id
else else
console.log 'debug', 'find not loaded!', @className, id console.log 'debug', 'find not loaded, load now!', @className, id
if callback 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 ]
for key, callback of @RETRIEVE_CALLBACK[ record.id ]
data = App[ @className ].find( record.id ) data = App[ @className ].find( record.id )
callback( data ) 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
@ -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) ->