From 18d04ea16fddc6d15639f6cb1b7448ffd0b8ff30 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Thu, 25 Oct 2012 18:42:41 +0200 Subject: [PATCH] Improved event bindings. --- .../_application_controller.js.coffee | 6 -- .../_dashboard/activity_stream.js.coffee | 7 +- .../app/controllers/_dashboard/rss.js.coffee | 9 +- .../controllers/_dashboard/ticket.js.coffee | 8 +- .../controllers/agent_ticket_view.js.coffee | 40 +++++---- app/assets/javascripts/app/index.js.coffee | 1 + .../javascripts/app/lib/collection.js.coffee | 6 +- .../javascripts/app/lib/event.js.coffee | 84 +++++++++++++++++++ .../app/lib/interface_handle.js.coffee | 4 + 9 files changed, 130 insertions(+), 35 deletions(-) create mode 100644 app/assets/javascripts/app/lib/event.js.coffee diff --git a/app/assets/javascripts/app/controllers/_application_controller.js.coffee b/app/assets/javascripts/app/controllers/_application_controller.js.coffee index 2205af542..9f37d40a8 100644 --- a/app/assets/javascripts/app/controllers/_application_controller.js.coffee +++ b/app/assets/javascripts/app/controllers/_application_controller.js.coffee @@ -20,12 +20,6 @@ class App.Controller extends Spine.Controller @delay( a, 0 ) - reBind: (name, callback) => - Spine.one name, (data) => - @log 'rebind', name, data - callback(data) - @reBind(name, callback) - isRole: (name) -> return false if !window.Session.roles for role in window.Session.roles diff --git a/app/assets/javascripts/app/controllers/_dashboard/activity_stream.js.coffee b/app/assets/javascripts/app/controllers/_dashboard/activity_stream.js.coffee index bc344828f..5ea1146ea 100644 --- a/app/assets/javascripts/app/controllers/_dashboard/activity_stream.js.coffee +++ b/app/assets/javascripts/app/controllers/_dashboard/activity_stream.js.coffee @@ -12,7 +12,12 @@ class App.DashboardActivityStream extends App.Controller # @interval( @fetch, 1400000, 'dashboard_activity_stream' ) @fetch() - @reBind( 'activity_stream_rebuild', @load ) + # bind to rebuild view event + App.Event.bind( + level: 'page' + event: 'activity_stream_rebuild' + callback: @load + ) fetch: => diff --git a/app/assets/javascripts/app/controllers/_dashboard/rss.js.coffee b/app/assets/javascripts/app/controllers/_dashboard/rss.js.coffee index b010f3591..ab5c3fb94 100644 --- a/app/assets/javascripts/app/controllers/_dashboard/rss.js.coffee +++ b/app/assets/javascripts/app/controllers/_dashboard/rss.js.coffee @@ -4,9 +4,12 @@ class App.DashboardRss extends App.Controller constructor: -> super - - # bind to new events - @reBind( 'rss_rebuild', @load ) + # bind to rebuild view event + App.Event.bind( + level: 'page' + event: 'rss_rebuild' + callback: @load + ) # refresh list ever 600 sec. @fetch() diff --git a/app/assets/javascripts/app/controllers/_dashboard/ticket.js.coffee b/app/assets/javascripts/app/controllers/_dashboard/ticket.js.coffee index 02a7867dc..1bc666853 100644 --- a/app/assets/javascripts/app/controllers/_dashboard/ticket.js.coffee +++ b/app/assets/javascripts/app/controllers/_dashboard/ticket.js.coffee @@ -14,8 +14,12 @@ class App.DashboardTicket extends App.Controller # set new key @key = 'ticket_overview_' + @view - # bind new events - @reBind('ticket_overview_rebuild', @fetch ) + # bind to rebuild view event + App.Event.bind( + level: 'page' + event: 'ticket_overview_rebuild' + callback: @fetch + ) # render @fetch() diff --git a/app/assets/javascripts/app/controllers/agent_ticket_view.js.coffee b/app/assets/javascripts/app/controllers/agent_ticket_view.js.coffee index 3ff7a684a..46d5b3ddf 100644 --- a/app/assets/javascripts/app/controllers/agent_ticket_view.js.coffee +++ b/app/assets/javascripts/app/controllers/agent_ticket_view.js.coffee @@ -23,14 +23,15 @@ class Index extends App.Controller @meta = {} @bulk = {} - # set controller to active - Config['ActiveController'] = '#ticket_overview_' + @view - # set new key @key = 'ticket_overview_' + @view - # bind new events - @reBind( 'ticket_overview_rebuild', @fetch ) + # bind to rebuild view event + App.Event.bind( + level: 'page' + event: 'ticket_overview_rebuild' + callback: @fetch + ) # render @fetch() @@ -45,20 +46,6 @@ class Index extends App.Controller @ticket_list = cache.ticket_list @load(cache) - # get data -# App.Com.ajax( -# id: 'ticket_overview_' + @start_page, -# type: 'GET', -# url: '/api/ticket_overviews', -# data: { -# view: @view, -# view_mode: @view_mode, -# start_page: @start_page, -# } -# processData: true, -# success: @load -# ) - load: (data) => # get meta data @@ -75,6 +62,19 @@ class Index extends App.Controller @log 'refetch...', record @fetch() +# # bind render after a change is done +# App.Collection.observe( +# level: 'page', +# collections: [ +# { +# collection: @genericObject, +# event: 'refresh change', +# callback: @render, +# }, +# ], +# ) + + @ticket_list_show = [] for ticket_id in @ticket_list @ticket_list_show.push App.Collection.find( 'Ticket', ticket_id ) @@ -90,8 +90,6 @@ class Index extends App.Controller render: -> - return if Config['ActiveController'] isnt '#ticket_overview_' + @view - # set page title @title @overview.meta.name diff --git a/app/assets/javascripts/app/index.js.coffee b/app/assets/javascripts/app/index.js.coffee index a2b44eff8..4b61edd96 100644 --- a/app/assets/javascripts/app/index.js.coffee +++ b/app/assets/javascripts/app/index.js.coffee @@ -30,6 +30,7 @@ #= require ./lib/i18n.js.coffee #= require ./lib/store.js.coffee #= require ./lib/collection.js.coffee +#= require ./lib/event.js.coffee #= require_tree ./models #= require_tree ./controllers #= require_tree ./views diff --git a/app/assets/javascripts/app/lib/collection.js.coffee b/app/assets/javascripts/app/lib/collection.js.coffee index fd0cd1493..b6007d403 100644 --- a/app/assets/javascripts/app/lib/collection.js.coffee +++ b/app/assets/javascripts/app/lib/collection.js.coffee @@ -334,11 +334,13 @@ class _Singleton for observe in data.collections events = observe.event.split(' ') for event in events - App[ observe.collection ].bind( event, observe.callback ) + if App[ observe.collection ] + App[ observe.collection ].bind( event, observe.callback ) _observeUnbind: (observers) -> for observe in observers events = observe.event.split(' ') for event in events - App[ observe.collection ].unbind( event, observe.callback ) + if App[ observe.collection ] + App[ observe.collection ].unbind( event, observe.callback ) diff --git a/app/assets/javascripts/app/lib/event.js.coffee b/app/assets/javascripts/app/lib/event.js.coffee new file mode 100644 index 000000000..df7cfff2b --- /dev/null +++ b/app/assets/javascripts/app/lib/event.js.coffee @@ -0,0 +1,84 @@ +class App.Event + _instance = undefined + + @init: -> + _instance = new _Singleton + + @bind: (args) -> + if _instance == undefined + _instance ?= new _Singleton + _instance.bind(args) + + @unbind: (args) -> + if _instance == undefined + _instance ?= new _Singleton + _instance.unbind(args) + + + @cleanUpLevel: (level) -> + if _instance == undefined + _instance ?= new _Singleton + _instance.cleanUpLevel(level) + +class _Singleton + + constructor: (args) -> + super + @eventCurrent = {} + + cleanUpLevel: (level) -> + return if !@eventCurrent[level] + for event of @eventCurrent[level] + @_unbind( level, event ) + + bind: (data) -> + + if !data.level + Spine.bind( data.event, data.callback ) + return + + if !@eventCurrent[ data.level ] + @eventCurrent[ data.level ] = {} + + # for all events + events = data.event.split(' ') + for event in events + + # unbind + @_unbind( data.level, event ) + + for event in events + + # remember all events + @eventCurrent[ data.level ][ event ] = data + + # bind + Spine.bind( event, data.callback ) + + _unbind: ( level, event ) -> + console.log '_unbind', level, event + return if !@eventCurrent[level] + + data = @eventCurrent[ level ][ event ] + return if !data + + Spine.unbind( event, data.callback ) + + @eventCurrent[ level ][ event ] = undefined + + unbind: (data) -> + + if !data.level + Spine.unbind( data.event, data.callback ) + return + + if !@eventCurrent[ data.level ] + @eventCurrent[ data.level ] = {} + + # for all events + events = data.event.split(' ') + for event in events + + # unbind + @_unbind( data.level, event ) + diff --git a/app/assets/javascripts/app/lib/interface_handle.js.coffee b/app/assets/javascripts/app/lib/interface_handle.js.coffee index 948a947f1..49117edcc 100644 --- a/app/assets/javascripts/app/lib/interface_handle.js.coffee +++ b/app/assets/javascripts/app/lib/interface_handle.js.coffee @@ -43,11 +43,15 @@ class App.Content extends Spine.Controller # remove observers for page App.Collection.observeCleanUpLevel('page') + # remove events for page + App.Event.cleanUpLevel('page') + # send current controller params_only = {} for i of params if typeof params[i] isnt 'object' params_only[i] = params[i] + App.WebSocket.send( action: 'active_controller', controller: route,