From db224d5bc30dc2bf1cec05e37bda6dbde625c987 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sun, 29 May 2016 01:53:12 +0200 Subject: [PATCH] Improved bind/unbind of controllers. --- .../controllers/agent_ticket_create.coffee | 13 +++++++---- .../app/controllers/ticket_overview.coffee | 3 +-- .../app/lib/app_post/_collection_base.coffee | 23 +++++++++++-------- .../app_post/overview_index_collection.coffee | 3 +++ .../app_post/ticket_create_collection.coffee | 3 +++ 5 files changed, 29 insertions(+), 16 deletions(-) diff --git a/app/assets/javascripts/app/controllers/agent_ticket_create.coffee b/app/assets/javascripts/app/controllers/agent_ticket_create.coffee index f3059179a..57aba6707 100644 --- a/app/assets/javascripts/app/controllers/agent_ticket_create.coffee +++ b/app/assets/javascripts/app/controllers/agent_ticket_create.coffee @@ -23,6 +23,12 @@ class App.TicketCreate extends App.Controller if @ticket_id && @article_id @split = "/#{@ticket_id}/#{@article_id}" + load = (data) => + App.Collection.loadAssets(data.assets) + @formMeta = data.form_meta + @buildScreen(params) + @bindId = App.TicketCreateCollection.one(load) + # lisen if view need to be rerendered @bind 'ticket_create_rerender', (defaults) => @log 'notice', 'error', defaults @@ -33,11 +39,8 @@ class App.TicketCreate extends App.Controller return if !@authenticate(true) @render() - load = (data) => - App.Collection.loadAssets(data.assets) - @formMeta = data.form_meta - @buildScreen(params) - @bindId = App.TicketCreateCollection.one(load) + release: => + App.TicketCreateCollection.unbindById(@bindId) currentChannel: => if !type diff --git a/app/assets/javascripts/app/controllers/ticket_overview.coffee b/app/assets/javascripts/app/controllers/ticket_overview.coffee index 6606e94c1..5181d47a2 100644 --- a/app/assets/javascripts/app/controllers/ticket_overview.coffee +++ b/app/assets/javascripts/app/controllers/ticket_overview.coffee @@ -126,8 +126,7 @@ class Navbar extends App.Controller release: => if @vertical $(window).off 'resize.navbar', @autoFoldTabs - if @bindId - App.OverviewIndexCollection.unbind(@bindId) + App.OverviewIndexCollection.unbindById(@bindId) autoFoldTabs: => items = App.OverviewIndexCollection.get() diff --git a/app/assets/javascripts/app/lib/app_post/_collection_base.coffee b/app/assets/javascripts/app/lib/app_post/_collection_base.coffee index 1f7389a1d..2baace9e2 100644 --- a/app/assets/javascripts/app/lib/app_post/_collection_base.coffee +++ b/app/assets/javascripts/app/lib/app_post/_collection_base.coffee @@ -16,33 +16,38 @@ class App._CollectionSingletonBase @set(data) @callback(data) - get: -> - @collection_data + get: => + @collectionData - set: (data) -> + set: (data) => App.SessionStorage.set("collection-#{@event}", data) - @collection_data = data + @collectionData = data - bind: (callback, init = true, one = false) -> + bind: (callback, init = true, one = false) => @counter += 1 + localCounter = @counter # start init call if needed if init - if @collection_data is undefined + if @collectionData is undefined @fetch() else - callback(@collection_data) + callback(@collectionData) return if one - @callbacks[@counter] = + @callbacks[localCounter] = callback: callback one: one + localCounter - unbind: (callback) -> + unbind: (callback) => for counter, attr of @callbacks if callback is attr.callback delete @callbacks[counter] + unbindById: (counter) => + delete @callbacks[counter] + fetch: => if App.WebSocket.support() App.WebSocket.send(event: @event) diff --git a/app/assets/javascripts/app/lib/app_post/overview_index_collection.coffee b/app/assets/javascripts/app/lib/app_post/overview_index_collection.coffee index d0664dd14..741175b09 100644 --- a/app/assets/javascripts/app/lib/app_post/overview_index_collection.coffee +++ b/app/assets/javascripts/app/lib/app_post/overview_index_collection.coffee @@ -17,6 +17,9 @@ class App.OverviewIndexCollection @unbind: (callback) -> _instance.unbind(callback) + @unbindById: (id) -> + _instance.unbindById(id) + @trigger: -> _instance.trigger() diff --git a/app/assets/javascripts/app/lib/app_post/ticket_create_collection.coffee b/app/assets/javascripts/app/lib/app_post/ticket_create_collection.coffee index 623b1290e..a4ff9e0bd 100644 --- a/app/assets/javascripts/app/lib/app_post/ticket_create_collection.coffee +++ b/app/assets/javascripts/app/lib/app_post/ticket_create_collection.coffee @@ -17,6 +17,9 @@ class App.TicketCreateCollection @unbind: (callback) -> _instance.unbind(callback) + @unbindById: (id) -> + _instance.unbindById(id) + @trigger: -> _instance.trigger()