Improved event handling.
This commit is contained in:
parent
18d04ea16f
commit
ac05ce5121
18 changed files with 89 additions and 107 deletions
|
@ -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',
|
||||
|
|
|
@ -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: =>
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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.'
|
||||
|
|
|
@ -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.'),
|
||||
}
|
||||
|
|
|
@ -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'
|
||||
|
|
|
@ -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 )
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
|
@ -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.',
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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] )
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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')
|
||||
|
|
|
@ -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) =>
|
||||
|
|
Loading…
Reference in a new issue