From ac05ce51215d752c507259de66ca4353cc2199c3 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Thu, 25 Oct 2012 23:03:19 +0200 Subject: [PATCH] Improved event handling. --- .../_application_controller.js.coffee | 6 +- .../_dashboard/activity_stream.js.coffee | 6 +- .../app/controllers/_dashboard/rss.js.coffee | 6 +- .../controllers/_dashboard/ticket.js.coffee | 6 +- .../controllers/agent_ticket_create.js.coffee | 2 +- .../controllers/agent_ticket_view.js.coffee | 6 +- .../app/controllers/getting_started.js.coffee | 2 +- .../app/controllers/login.js.coffee | 10 +- .../app/controllers/logout.js.coffee | 2 +- .../app/controllers/navigation.js.coffee | 8 +- .../app/controllers/notify.js.coffee | 4 +- .../app/controllers/signup.js.coffee | 6 +- .../app/controllers/template.js.coffee | 2 +- app/assets/javascripts/app/lib/auth.js.coffee | 6 +- .../javascripts/app/lib/collection.js.coffee | 10 +- .../javascripts/app/lib/event.js.coffee | 98 +++++++++---------- .../app/lib/interface_handle.js.coffee | 10 +- .../javascripts/app/lib/websocket.js.coffee | 6 +- 18 files changed, 89 insertions(+), 107 deletions(-) diff --git a/app/assets/javascripts/app/controllers/_application_controller.js.coffee b/app/assets/javascripts/app/controllers/_application_controller.js.coffee index 9f37d40a8..4539124b9 100644 --- a/app/assets/javascripts/app/controllers/_application_controller.js.coffee +++ b/app/assets/javascripts/app/controllers/_application_controller.js.coffee @@ -7,11 +7,11 @@ class App.Controller extends Spine.Controller # add @notify methode to create notification notify: (data) -> - Spine.trigger 'notify', data + App.Event.trigger 'notify', data # add @navupdate methode to update navigation navupdate: (url) -> - Spine.trigger 'navupdate', url + App.Event.trigger 'navupdate', url scrollTo: ( x = 0, y = 0 ) -> a = -> @@ -261,7 +261,7 @@ class App.Controller extends Spine.Controller ) ws_send: (data) -> - Spine.trigger( 'ws:send', JSON.stringify(data) ) + App.Event.trigger( 'ws:send', JSON.stringify(data) ) class App.ControllerModal extends App.Controller className: 'modal hide fade', 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 5ea1146ea..4113b681e 100644 --- a/app/assets/javascripts/app/controllers/_dashboard/activity_stream.js.coffee +++ b/app/assets/javascripts/app/controllers/_dashboard/activity_stream.js.coffee @@ -13,11 +13,7 @@ class App.DashboardActivityStream extends App.Controller @fetch() # bind to rebuild view event - App.Event.bind( - level: 'page' - event: 'activity_stream_rebuild' - callback: @load - ) + App.Event.bind( 'activity_stream_rebuild', @load, 'page' ) fetch: => diff --git a/app/assets/javascripts/app/controllers/_dashboard/rss.js.coffee b/app/assets/javascripts/app/controllers/_dashboard/rss.js.coffee index ab5c3fb94..d95af74bd 100644 --- a/app/assets/javascripts/app/controllers/_dashboard/rss.js.coffee +++ b/app/assets/javascripts/app/controllers/_dashboard/rss.js.coffee @@ -5,11 +5,7 @@ class App.DashboardRss extends App.Controller super # bind to rebuild view event - App.Event.bind( - level: 'page' - event: 'rss_rebuild' - callback: @load - ) + App.Event.bind( 'rss_rebuild', @load, 'page' ) # 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 1bc666853..2928f11e1 100644 --- a/app/assets/javascripts/app/controllers/_dashboard/ticket.js.coffee +++ b/app/assets/javascripts/app/controllers/_dashboard/ticket.js.coffee @@ -15,11 +15,7 @@ class App.DashboardTicket extends App.Controller @key = 'ticket_overview_' + @view # bind to rebuild view event - App.Event.bind( - level: 'page' - event: 'ticket_overview_rebuild' - callback: @fetch - ) + App.Event.bind( 'ticket_overview_rebuild', @fetch, 'page' ) # render @fetch() diff --git a/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee b/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee index 5fc0811bb..2ba59cef8 100644 --- a/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee +++ b/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee @@ -21,7 +21,7 @@ class Index extends App.Controller @edit_form = undefined # lisen if view need to be rerendert - Spine.bind 'ticket_create_rerender', (defaults) => + App.Event.bind 'ticket_create_rerender', (defaults) => @log 'rerender', defaults @render(defaults) 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 46d5b3ddf..e96f5c485 100644 --- a/app/assets/javascripts/app/controllers/agent_ticket_view.js.coffee +++ b/app/assets/javascripts/app/controllers/agent_ticket_view.js.coffee @@ -27,11 +27,7 @@ class Index extends App.Controller @key = 'ticket_overview_' + @view # bind to rebuild view event - App.Event.bind( - level: 'page' - event: 'ticket_overview_rebuild' - callback: @fetch - ) + App.Event.bind( 'ticket_overview_rebuild', @fetch, 'page' ) # render @fetch() diff --git a/app/assets/javascripts/app/controllers/getting_started.js.coffee b/app/assets/javascripts/app/controllers/getting_started.js.coffee index f0bfc94db..dbfe7d8cf 100644 --- a/app/assets/javascripts/app/controllers/getting_started.js.coffee +++ b/app/assets/javascripts/app/controllers/getting_started.js.coffee @@ -125,7 +125,7 @@ class Index extends App.Controller App.Auth.loginCheck() # add notify - Spine.trigger 'notify:removeall' + App.Event.trigger 'notify:removeall' # @notify # type: 'success', # msg: 'Thanks for joining. Email sent to "' + @params.email + '". Please verify your email address.' diff --git a/app/assets/javascripts/app/controllers/login.js.coffee b/app/assets/javascripts/app/controllers/login.js.coffee index 4344e4be7..929ad4fbb 100644 --- a/app/assets/javascripts/app/controllers/login.js.coffee +++ b/app/assets/javascripts/app/controllers/login.js.coffee @@ -86,7 +86,7 @@ class Index extends App.Controller App[key].refresh( value, options: { clear: true } ) # rebuild navbar with user data - Spine.trigger 'navrebuild', data.session + App.Event.trigger 'navrebuild', data.session # update websocked auth info App.WebSocket.auth() @@ -95,8 +95,8 @@ class Index extends App.Controller App.WebSocket.send( event: 'navupdate_ticket_overview' ) # add notify - Spine.trigger 'notify:removeall' - Spine.trigger 'notify', { + App.Event.trigger 'notify:removeall' + App.Event.trigger 'notify', { type: 'success', msg: App.i18n.translateContent('Login successfully! Have a nice day!'), } @@ -114,8 +114,8 @@ class Index extends App.Controller console.log 'login:error' # add notify - Spine.trigger 'notify:removeall' - Spine.trigger 'notify', { + App.Event.trigger 'notify:removeall' + App.Event.trigger 'notify', { type: 'error', msg: App.i18n.translateContent('Wrong Username and Password combination.'), } diff --git a/app/assets/javascripts/app/controllers/logout.js.coffee b/app/assets/javascripts/app/controllers/logout.js.coffee index e3ff8aa7e..6dae57c5e 100644 --- a/app/assets/javascripts/app/controllers/logout.js.coffee +++ b/app/assets/javascripts/app/controllers/logout.js.coffee @@ -15,7 +15,7 @@ class Index extends Spine.Controller @log 'Session', window.Session window.Session = {} @log 'Session', window.Session - Spine.trigger 'navrebuild' + App.Event.trigger 'navrebuild' # redirect to login @navigate 'login' diff --git a/app/assets/javascripts/app/controllers/navigation.js.coffee b/app/assets/javascripts/app/controllers/navigation.js.coffee index 58cd1f0e5..1dd40047e 100644 --- a/app/assets/javascripts/app/controllers/navigation.js.coffee +++ b/app/assets/javascripts/app/controllers/navigation.js.coffee @@ -7,11 +7,11 @@ class App.Navigation extends App.Controller @render() # update selected item - Spine.bind 'navupdate', (data) => + App.Event.bind 'navupdate', (data) => @update(arguments[0]) # rebuild nav bar with given user data - Spine.bind 'navrebuild', (user) => + App.Event.bind 'navrebuild', (user) => @log 'navbarrebuild', user if !_.isEmpty( user ) @@ -25,12 +25,12 @@ class App.Navigation extends App.Controller @render(user) # rebuild ticket overview data - Spine.bind 'navupdate_ticket_overview', (data) => + App.Event.bind 'navupdate_ticket_overview', (data) => @ticket_overview_build(data) @render( window.Session ) # rebuild recent viewd data - Spine.bind 'update_recent_viewed', (data) => + App.Event.bind 'update_recent_viewed', (data) => @recent_viewed_build(data) @render( window.Session ) diff --git a/app/assets/javascripts/app/controllers/notify.js.coffee b/app/assets/javascripts/app/controllers/notify.js.coffee index 4566eca15..99e975e46 100644 --- a/app/assets/javascripts/app/controllers/notify.js.coffee +++ b/app/assets/javascripts/app/controllers/notify.js.coffee @@ -9,10 +9,10 @@ class App.Notify extends Spine.Controller constructor: -> super - Spine.bind 'notify', (data) => + App.Event.bind 'notify', (data) => @render(data) - Spine.bind 'notify:removeall', => + App.Event.bind 'notify:removeall', => @log 'notify:removeall', @ @destroyAll() diff --git a/app/assets/javascripts/app/controllers/signup.js.coffee b/app/assets/javascripts/app/controllers/signup.js.coffee index ef97d011e..d8eac3fa9 100644 --- a/app/assets/javascripts/app/controllers/signup.js.coffee +++ b/app/assets/javascripts/app/controllers/signup.js.coffee @@ -77,7 +77,7 @@ class Index extends App.Controller App.Auth.loginCheck() # add notify - Spine.trigger 'notify:removeall' + App.Event.trigger 'notify:removeall' @notify type: 'success', msg: 'Thanks for joining. Email sent to "' + @params.email + '". Please verify your email address.' @@ -88,8 +88,8 @@ class Index extends App.Controller error: (xhr, statusText, error) => # add notify - Spine.trigger 'notify:removeall' - Spine.trigger 'notify', { + App.Event.trigger 'notify:removeall' + App.Event.trigger 'notify', { type: 'warning', msg: 'Wrong Username and Password combination.', } diff --git a/app/assets/javascripts/app/controllers/template.js.coffee b/app/assets/javascripts/app/controllers/template.js.coffee index b861ecb63..fe430a4d0 100644 --- a/app/assets/javascripts/app/controllers/template.js.coffee +++ b/app/assets/javascripts/app/controllers/template.js.coffee @@ -62,7 +62,7 @@ class App.TemplateUI extends App.Controller params = @formParam(e.target) template = App.Collection.find( 'Template', params['template_id'] ) - Spine.trigger 'ticket_create_rerender', template.attributes() + App.Event.trigger 'ticket_create_rerender', template.attributes() create: (e) => e.preventDefault() diff --git a/app/assets/javascripts/app/lib/auth.js.coffee b/app/assets/javascripts/app/lib/auth.js.coffee index 29a67b767..5beec7d18 100644 --- a/app/assets/javascripts/app/lib/auth.js.coffee +++ b/app/assets/javascripts/app/lib/auth.js.coffee @@ -45,7 +45,7 @@ class App.Auth App.WebSocket.auth() # rebuild navbar with new navbar items - Spine.trigger 'navrebuild' + App.Event.trigger 'navrebuild' return false; @@ -69,10 +69,10 @@ class App.Auth App.Collection.reset( type: key, data: value ) # rebuild navbar with new navbar items - Spine.trigger 'navrebuild', data.session + App.Event.trigger 'navrebuild', data.session # rebuild navbar with updated ticket count of overviews - Spine.trigger 'navupdate_remote' + App.Event.trigger 'navupdate_remote' error: (xhr, statusText, error) => console.log 'loginCheck:error'#, error, statusText, xhr.statusCode diff --git a/app/assets/javascripts/app/lib/collection.js.coffee b/app/assets/javascripts/app/lib/collection.js.coffee index b6007d403..c2a806627 100644 --- a/app/assets/javascripts/app/lib/collection.js.coffee +++ b/app/assets/javascripts/app/lib/collection.js.coffee @@ -54,17 +54,17 @@ class App.Collection _instance ?= new _Singleton _instance.observe(args) - @observeCleanUpLevel: (level) -> + @observeUnbindLevel: (level) -> if _instance == undefined _instance ?= new _Singleton - _instance.observeCleanUpLevel(level) + _instance.observeUnbindLevel(level) class _Singleton constructor: (@args) -> # add trigger - bind new events - Spine.bind 'loadCollection', (data) => + App.Event.bind 'loadCollection', (data) => # load collections if data.collections @@ -74,7 +74,7 @@ class _Singleton @load( localStorage: data.localStorage, type: type, data: data.collections[type] ) # add trigger - bind new events - Spine.bind 'resetCollection', (data) => + App.Event.bind 'resetCollection', (data) => # load collections if data.collections @@ -318,7 +318,7 @@ class _Singleton ) return collection - observeCleanUpLevel: (level) -> + observeUnbindLevel: (level) -> return if !@observeCurrent return if !@observeCurrent[level] @_observeUnbind( @observeCurrent[level] ) diff --git a/app/assets/javascripts/app/lib/event.js.coffee b/app/assets/javascripts/app/lib/event.js.coffee index df7cfff2b..3bbfe3b56 100644 --- a/app/assets/javascripts/app/lib/event.js.coffee +++ b/app/assets/javascripts/app/lib/event.js.coffee @@ -4,81 +4,79 @@ class App.Event @init: -> _instance = new _Singleton - @bind: (args) -> + @bind: ( events, callback, level ) -> if _instance == undefined _instance ?= new _Singleton - _instance.bind(args) + _instance.bind( events, callback, level ) - @unbind: (args) -> + @unbind: ( events, callback, level ) -> if _instance == undefined _instance ?= new _Singleton - _instance.unbind(args) + _instance.unbind( events, callback, level ) - - @cleanUpLevel: (level) -> + @trigger: ( events, data ) -> if _instance == undefined _instance ?= new _Singleton - _instance.cleanUpLevel(level) + _instance.trigger( events, data ) + + @unbindLevel: (level) -> + if _instance == undefined + _instance ?= new _Singleton + _instance.unbindLevel(level) class _Singleton - constructor: (args) -> - super + constructor: -> @eventCurrent = {} - cleanUpLevel: (level) -> + unbindLevel: (level) -> return if !@eventCurrent[level] - for event of @eventCurrent[level] - @_unbind( level, event ) + for item in @eventCurrent[level] + @unbind( item.event, item.callback, level ) + @eventCurrent[level] = [] - bind: (data) -> + bind: ( events, callback, level ) -> - if !data.level - Spine.bind( data.event, data.callback ) - return + if !level + level = '_all' - if !@eventCurrent[ data.level ] - @eventCurrent[ data.level ] = {} + if !@eventCurrent[level] + @eventCurrent[level] = [] - # for all events - events = data.event.split(' ') - for event in events - - # unbind - @_unbind( data.level, event ) - - for event in events + # level boundary events + eventList = events.split(' ') + for event in eventList # remember all events - @eventCurrent[ data.level ][ event ] = data + @eventCurrent[ level ].push { + event: event, + callback: callback, + } # bind - Spine.bind( event, data.callback ) + Spine.bind( event, callback ) - _unbind: ( level, event ) -> - console.log '_unbind', level, event - return if !@eventCurrent[level] + unbind: ( events, callback, level ) -> - data = @eventCurrent[ level ][ event ] - return if !data + if !level + level = '_all' - Spine.unbind( event, data.callback ) + if !@eventCurrent[level] + @eventCurrent[level] = [] - @eventCurrent[ level ][ event ] = undefined + eventList = events.split(' ') + for event in eventList - 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 ) + # remove from + @eventCurrent[level] = _.filter( @eventCurrent[level], (item) -> + if callback + return item if item.event isnt event && item.callback isnt callback + else + return item if item.event isnt event + ) + Spine.unbind( event, callback ) + trigger: ( events, data ) -> + eventList = events.split(' ') + for event in eventList + Spine.trigger event, data diff --git a/app/assets/javascripts/app/lib/interface_handle.js.coffee b/app/assets/javascripts/app/lib/interface_handle.js.coffee index 49117edcc..7147b21d8 100644 --- a/app/assets/javascripts/app/lib/interface_handle.js.coffee +++ b/app/assets/javascripts/app/lib/interface_handle.js.coffee @@ -34,17 +34,17 @@ class App.Content extends Spine.Controller constructor: -> super - @log 'RUN content'#, @ + @log 'RUN content' for route, callback of Config.Routes do (route, callback) => @route(route, (params) -> # remove observers for page - App.Collection.observeCleanUpLevel('page') + App.Collection.observeUnbindLevel('page') # remove events for page - App.Event.cleanUpLevel('page') + App.Event.unbindLevel('page') # send current controller params_only = {} @@ -59,8 +59,8 @@ class App.Content extends Spine.Controller ) # unbind in controller area - @el.unbind() - @el.undelegate() +# @el.unbind() +# @el.undelegate() # remove waypoints $('footer').waypoint('remove') diff --git a/app/assets/javascripts/app/lib/websocket.js.coffee b/app/assets/javascripts/app/lib/websocket.js.coffee index 16424912b..d3e7d3cb7 100644 --- a/app/assets/javascripts/app/lib/websocket.js.coffee +++ b/app/assets/javascripts/app/lib/websocket.js.coffee @@ -138,13 +138,13 @@ class _Singleton extends Spine.Controller if typeof item['event'] is 'object' for event in item['event'] console.log( "ws:onmessage event:" + event ) - Spine.trigger( event, item['data'] ) + App.Event.trigger( event, item['data'] ) else console.log( "ws:onmessage event:" + item['event'] ) - Spine.trigger( item['event'], item['data'] ) + App.Event.trigger( item['event'], item['data'] ) # bind to send messages - Spine.bind 'ws:send', (data) => + App.Event.bind 'ws:send', (data) => @send(data) @ws.onclose = (e) =>