From 8612c1a0193f30c6a2315e4cb42f2541907dd86e Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Thu, 29 Sep 2016 10:04:11 +0200 Subject: [PATCH] Only execute collection lookup via App.Model.subscribe min. callback exists. --- .../app/models/_application_model.coffee | 45 ++++++++++--------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/app/assets/javascripts/app/models/_application_model.coffee b/app/assets/javascripts/app/models/_application_model.coffee index ff1667e39..7fac50188 100644 --- a/app/assets/javascripts/app/models/_application_model.coffee +++ b/app/assets/javascripts/app/models/_application_model.coffee @@ -233,7 +233,7 @@ class App.Model extends Spine.Model # subscribe and reload data / fetch new data if triggered subscribeId = undefined if bind - subscribeId = App[ @className ].subscribe_item(id, callback) + subscribeId = App[ @className ].subscribeItem(id, callback) # execute if object already exists if !force && App[ @className ].exists(id) @@ -345,6 +345,7 @@ class App.Model extends Spine.Model App.Event.bind( events => + return if _.isEmpty(@SUBSCRIPTION_COLLECTION) App.Log.debug('Model', "server notify collection change #{@className}") @fetchFull( -> @@ -394,13 +395,13 @@ class App.Model extends Spine.Model subscribe: (callback, type) -> # remember record id and callback - App[ @constructor.className ].subscribe_item(@id, callback) + App[ @constructor.className ].subscribeItem(@id, callback) - @subscribe_item: (id, callback) -> + @subscribeItem: (id, callback) -> # init bind - if !@_subscribe_item_bindDone - @_subscribe_item_bindDone = true + if !@_subscribeItemBindDone + @_subscribeItemBindDone = true # subscribe and render data after local change @bind( @@ -454,16 +455,16 @@ class App.Model extends Spine.Model App.Event.bind( events (item) => - if @SUBSCRIPTION_ITEM && @SUBSCRIPTION_ITEM[ item.id ] - App.Log.debug('Model', "server change on #{@className}.find(#{item.id}) #{item.updated_at}") - callback = => - genericObject = undefined - if App[ @className ].exists(item.id) - genericObject = App[ @className ].find(item.id) - if !genericObject || new Date(item.updated_at) > new Date(genericObject.updated_at) - App.Log.debug('Model', "request #{@className}.find(#{item.id}) from server") - @full(item.id, false, true) - App.Delay.set(callback, 600, item.id, "full-#{@className}-#{item.id}") + return if !@SUBSCRIPTION_ITEM || !@SUBSCRIPTION_ITEM[ item.id ] + App.Log.debug('Model', "server change on #{@className}.find(#{item.id}) #{item.updated_at}") + callback = => + genericObject = undefined + if App[ @className ].exists(item.id) + genericObject = App[ @className ].find(item.id) + if !genericObject || new Date(item.updated_at) > new Date(genericObject.updated_at) + App.Log.debug('Model', "request #{@className}.find(#{item.id}) from server") + @full(item.id, false, true) + App.Delay.set(callback, 600, item.id, "full-#{@className}-#{item.id}") "Item::Subscribe::#{@className}" ) @@ -471,13 +472,13 @@ class App.Model extends Spine.Model App.Event.bind( events (item) => - if @SUBSCRIPTION_ITEM && @SUBSCRIPTION_ITEM[ item.id ] - return if !App[ @className ].exists(item.id) - genericObject = App[ @className ].find(item.id) - App.Log.debug('Model', "server delete on #{@className}.find(#{item.id}) #{item.updated_at}") - callback = -> - genericObject.trigger('destroy', genericObject) - App.Delay.set(callback, 500, item.id, "delete-#{@className}-#{item.id}") + return if !@SUBSCRIPTION_ITEM || !@SUBSCRIPTION_ITEM[ item.id ] + return if !App[ @className ].exists(item.id) + genericObject = App[ @className ].find(item.id) + App.Log.debug('Model', "server delete on #{@className}.find(#{item.id}) #{item.updated_at}") + callback = -> + genericObject.trigger('destroy', genericObject) + App.Delay.set(callback, 500, item.id, "delete-#{@className}-#{item.id}") "Item::SubscribeDelete::#{@className}" )