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
|
# add @notify methode to create notification
|
||||||
notify: (data) ->
|
notify: (data) ->
|
||||||
Spine.trigger 'notify', data
|
App.Event.trigger 'notify', data
|
||||||
|
|
||||||
# add @navupdate methode to update navigation
|
# add @navupdate methode to update navigation
|
||||||
navupdate: (url) ->
|
navupdate: (url) ->
|
||||||
Spine.trigger 'navupdate', url
|
App.Event.trigger 'navupdate', url
|
||||||
|
|
||||||
scrollTo: ( x = 0, y = 0 ) ->
|
scrollTo: ( x = 0, y = 0 ) ->
|
||||||
a = ->
|
a = ->
|
||||||
|
@ -261,7 +261,7 @@ class App.Controller extends Spine.Controller
|
||||||
)
|
)
|
||||||
|
|
||||||
ws_send: (data) ->
|
ws_send: (data) ->
|
||||||
Spine.trigger( 'ws:send', JSON.stringify(data) )
|
App.Event.trigger( 'ws:send', JSON.stringify(data) )
|
||||||
|
|
||||||
class App.ControllerModal extends App.Controller
|
class App.ControllerModal extends App.Controller
|
||||||
className: 'modal hide fade',
|
className: 'modal hide fade',
|
||||||
|
|
|
@ -13,11 +13,7 @@ class App.DashboardActivityStream extends App.Controller
|
||||||
@fetch()
|
@fetch()
|
||||||
|
|
||||||
# bind to rebuild view event
|
# bind to rebuild view event
|
||||||
App.Event.bind(
|
App.Event.bind( 'activity_stream_rebuild', @load, 'page' )
|
||||||
level: 'page'
|
|
||||||
event: 'activity_stream_rebuild'
|
|
||||||
callback: @load
|
|
||||||
)
|
|
||||||
|
|
||||||
fetch: =>
|
fetch: =>
|
||||||
|
|
||||||
|
|
|
@ -5,11 +5,7 @@ class App.DashboardRss extends App.Controller
|
||||||
super
|
super
|
||||||
|
|
||||||
# bind to rebuild view event
|
# bind to rebuild view event
|
||||||
App.Event.bind(
|
App.Event.bind( 'rss_rebuild', @load, 'page' )
|
||||||
level: 'page'
|
|
||||||
event: 'rss_rebuild'
|
|
||||||
callback: @load
|
|
||||||
)
|
|
||||||
|
|
||||||
# refresh list ever 600 sec.
|
# refresh list ever 600 sec.
|
||||||
@fetch()
|
@fetch()
|
||||||
|
|
|
@ -15,11 +15,7 @@ class App.DashboardTicket extends App.Controller
|
||||||
@key = 'ticket_overview_' + @view
|
@key = 'ticket_overview_' + @view
|
||||||
|
|
||||||
# bind to rebuild view event
|
# bind to rebuild view event
|
||||||
App.Event.bind(
|
App.Event.bind( 'ticket_overview_rebuild', @fetch, 'page' )
|
||||||
level: 'page'
|
|
||||||
event: 'ticket_overview_rebuild'
|
|
||||||
callback: @fetch
|
|
||||||
)
|
|
||||||
|
|
||||||
# render
|
# render
|
||||||
@fetch()
|
@fetch()
|
||||||
|
|
|
@ -21,7 +21,7 @@ class Index extends App.Controller
|
||||||
@edit_form = undefined
|
@edit_form = undefined
|
||||||
|
|
||||||
# lisen if view need to be rerendert
|
# lisen if view need to be rerendert
|
||||||
Spine.bind 'ticket_create_rerender', (defaults) =>
|
App.Event.bind 'ticket_create_rerender', (defaults) =>
|
||||||
@log 'rerender', defaults
|
@log 'rerender', defaults
|
||||||
@render(defaults)
|
@render(defaults)
|
||||||
|
|
||||||
|
|
|
@ -27,11 +27,7 @@ class Index extends App.Controller
|
||||||
@key = 'ticket_overview_' + @view
|
@key = 'ticket_overview_' + @view
|
||||||
|
|
||||||
# bind to rebuild view event
|
# bind to rebuild view event
|
||||||
App.Event.bind(
|
App.Event.bind( 'ticket_overview_rebuild', @fetch, 'page' )
|
||||||
level: 'page'
|
|
||||||
event: 'ticket_overview_rebuild'
|
|
||||||
callback: @fetch
|
|
||||||
)
|
|
||||||
|
|
||||||
# render
|
# render
|
||||||
@fetch()
|
@fetch()
|
||||||
|
|
|
@ -125,7 +125,7 @@ class Index extends App.Controller
|
||||||
App.Auth.loginCheck()
|
App.Auth.loginCheck()
|
||||||
|
|
||||||
# add notify
|
# add notify
|
||||||
Spine.trigger 'notify:removeall'
|
App.Event.trigger 'notify:removeall'
|
||||||
# @notify
|
# @notify
|
||||||
# type: 'success',
|
# type: 'success',
|
||||||
# msg: 'Thanks for joining. Email sent to "' + @params.email + '". Please verify your email address.'
|
# 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 } )
|
App[key].refresh( value, options: { clear: true } )
|
||||||
|
|
||||||
# rebuild navbar with user data
|
# rebuild navbar with user data
|
||||||
Spine.trigger 'navrebuild', data.session
|
App.Event.trigger 'navrebuild', data.session
|
||||||
|
|
||||||
# update websocked auth info
|
# update websocked auth info
|
||||||
App.WebSocket.auth()
|
App.WebSocket.auth()
|
||||||
|
@ -95,8 +95,8 @@ class Index extends App.Controller
|
||||||
App.WebSocket.send( event: 'navupdate_ticket_overview' )
|
App.WebSocket.send( event: 'navupdate_ticket_overview' )
|
||||||
|
|
||||||
# add notify
|
# add notify
|
||||||
Spine.trigger 'notify:removeall'
|
App.Event.trigger 'notify:removeall'
|
||||||
Spine.trigger 'notify', {
|
App.Event.trigger 'notify', {
|
||||||
type: 'success',
|
type: 'success',
|
||||||
msg: App.i18n.translateContent('Login successfully! Have a nice day!'),
|
msg: App.i18n.translateContent('Login successfully! Have a nice day!'),
|
||||||
}
|
}
|
||||||
|
@ -114,8 +114,8 @@ class Index extends App.Controller
|
||||||
console.log 'login:error'
|
console.log 'login:error'
|
||||||
|
|
||||||
# add notify
|
# add notify
|
||||||
Spine.trigger 'notify:removeall'
|
App.Event.trigger 'notify:removeall'
|
||||||
Spine.trigger 'notify', {
|
App.Event.trigger 'notify', {
|
||||||
type: 'error',
|
type: 'error',
|
||||||
msg: App.i18n.translateContent('Wrong Username and Password combination.'),
|
msg: App.i18n.translateContent('Wrong Username and Password combination.'),
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,7 @@ class Index extends Spine.Controller
|
||||||
@log 'Session', window.Session
|
@log 'Session', window.Session
|
||||||
window.Session = {}
|
window.Session = {}
|
||||||
@log 'Session', window.Session
|
@log 'Session', window.Session
|
||||||
Spine.trigger 'navrebuild'
|
App.Event.trigger 'navrebuild'
|
||||||
|
|
||||||
# redirect to login
|
# redirect to login
|
||||||
@navigate 'login'
|
@navigate 'login'
|
||||||
|
|
|
@ -7,11 +7,11 @@ class App.Navigation extends App.Controller
|
||||||
@render()
|
@render()
|
||||||
|
|
||||||
# update selected item
|
# update selected item
|
||||||
Spine.bind 'navupdate', (data) =>
|
App.Event.bind 'navupdate', (data) =>
|
||||||
@update(arguments[0])
|
@update(arguments[0])
|
||||||
|
|
||||||
# rebuild nav bar with given user data
|
# rebuild nav bar with given user data
|
||||||
Spine.bind 'navrebuild', (user) =>
|
App.Event.bind 'navrebuild', (user) =>
|
||||||
@log 'navbarrebuild', user
|
@log 'navbarrebuild', user
|
||||||
|
|
||||||
if !_.isEmpty( user )
|
if !_.isEmpty( user )
|
||||||
|
@ -25,12 +25,12 @@ class App.Navigation extends App.Controller
|
||||||
@render(user)
|
@render(user)
|
||||||
|
|
||||||
# rebuild ticket overview data
|
# rebuild ticket overview data
|
||||||
Spine.bind 'navupdate_ticket_overview', (data) =>
|
App.Event.bind 'navupdate_ticket_overview', (data) =>
|
||||||
@ticket_overview_build(data)
|
@ticket_overview_build(data)
|
||||||
@render( window.Session )
|
@render( window.Session )
|
||||||
|
|
||||||
# rebuild recent viewd data
|
# rebuild recent viewd data
|
||||||
Spine.bind 'update_recent_viewed', (data) =>
|
App.Event.bind 'update_recent_viewed', (data) =>
|
||||||
@recent_viewed_build(data)
|
@recent_viewed_build(data)
|
||||||
@render( window.Session )
|
@render( window.Session )
|
||||||
|
|
||||||
|
|
|
@ -9,10 +9,10 @@ class App.Notify extends Spine.Controller
|
||||||
constructor: ->
|
constructor: ->
|
||||||
super
|
super
|
||||||
|
|
||||||
Spine.bind 'notify', (data) =>
|
App.Event.bind 'notify', (data) =>
|
||||||
@render(data)
|
@render(data)
|
||||||
|
|
||||||
Spine.bind 'notify:removeall', =>
|
App.Event.bind 'notify:removeall', =>
|
||||||
@log 'notify:removeall', @
|
@log 'notify:removeall', @
|
||||||
@destroyAll()
|
@destroyAll()
|
||||||
|
|
||||||
|
|
|
@ -77,7 +77,7 @@ class Index extends App.Controller
|
||||||
App.Auth.loginCheck()
|
App.Auth.loginCheck()
|
||||||
|
|
||||||
# add notify
|
# add notify
|
||||||
Spine.trigger 'notify:removeall'
|
App.Event.trigger 'notify:removeall'
|
||||||
@notify
|
@notify
|
||||||
type: 'success',
|
type: 'success',
|
||||||
msg: 'Thanks for joining. Email sent to "' + @params.email + '". Please verify your email address.'
|
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) =>
|
error: (xhr, statusText, error) =>
|
||||||
|
|
||||||
# add notify
|
# add notify
|
||||||
Spine.trigger 'notify:removeall'
|
App.Event.trigger 'notify:removeall'
|
||||||
Spine.trigger 'notify', {
|
App.Event.trigger 'notify', {
|
||||||
type: 'warning',
|
type: 'warning',
|
||||||
msg: 'Wrong Username and Password combination.',
|
msg: 'Wrong Username and Password combination.',
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,7 +62,7 @@ class App.TemplateUI extends App.Controller
|
||||||
params = @formParam(e.target)
|
params = @formParam(e.target)
|
||||||
|
|
||||||
template = App.Collection.find( 'Template', params['template_id'] )
|
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) =>
|
create: (e) =>
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
|
|
|
@ -45,7 +45,7 @@ class App.Auth
|
||||||
App.WebSocket.auth()
|
App.WebSocket.auth()
|
||||||
|
|
||||||
# rebuild navbar with new navbar items
|
# rebuild navbar with new navbar items
|
||||||
Spine.trigger 'navrebuild'
|
App.Event.trigger 'navrebuild'
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
@ -69,10 +69,10 @@ class App.Auth
|
||||||
App.Collection.reset( type: key, data: value )
|
App.Collection.reset( type: key, data: value )
|
||||||
|
|
||||||
# rebuild navbar with new navbar items
|
# 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
|
# rebuild navbar with updated ticket count of overviews
|
||||||
Spine.trigger 'navupdate_remote'
|
App.Event.trigger 'navupdate_remote'
|
||||||
|
|
||||||
error: (xhr, statusText, error) =>
|
error: (xhr, statusText, error) =>
|
||||||
console.log 'loginCheck:error'#, error, statusText, xhr.statusCode
|
console.log 'loginCheck:error'#, error, statusText, xhr.statusCode
|
||||||
|
|
|
@ -54,17 +54,17 @@ class App.Collection
|
||||||
_instance ?= new _Singleton
|
_instance ?= new _Singleton
|
||||||
_instance.observe(args)
|
_instance.observe(args)
|
||||||
|
|
||||||
@observeCleanUpLevel: (level) ->
|
@observeUnbindLevel: (level) ->
|
||||||
if _instance == undefined
|
if _instance == undefined
|
||||||
_instance ?= new _Singleton
|
_instance ?= new _Singleton
|
||||||
_instance.observeCleanUpLevel(level)
|
_instance.observeUnbindLevel(level)
|
||||||
|
|
||||||
class _Singleton
|
class _Singleton
|
||||||
|
|
||||||
constructor: (@args) ->
|
constructor: (@args) ->
|
||||||
|
|
||||||
# add trigger - bind new events
|
# add trigger - bind new events
|
||||||
Spine.bind 'loadCollection', (data) =>
|
App.Event.bind 'loadCollection', (data) =>
|
||||||
|
|
||||||
# load collections
|
# load collections
|
||||||
if data.collections
|
if data.collections
|
||||||
|
@ -74,7 +74,7 @@ class _Singleton
|
||||||
@load( localStorage: data.localStorage, type: type, data: data.collections[type] )
|
@load( localStorage: data.localStorage, type: type, data: data.collections[type] )
|
||||||
|
|
||||||
# add trigger - bind new events
|
# add trigger - bind new events
|
||||||
Spine.bind 'resetCollection', (data) =>
|
App.Event.bind 'resetCollection', (data) =>
|
||||||
|
|
||||||
# load collections
|
# load collections
|
||||||
if data.collections
|
if data.collections
|
||||||
|
@ -318,7 +318,7 @@ class _Singleton
|
||||||
)
|
)
|
||||||
return collection
|
return collection
|
||||||
|
|
||||||
observeCleanUpLevel: (level) ->
|
observeUnbindLevel: (level) ->
|
||||||
return if !@observeCurrent
|
return if !@observeCurrent
|
||||||
return if !@observeCurrent[level]
|
return if !@observeCurrent[level]
|
||||||
@_observeUnbind( @observeCurrent[level] )
|
@_observeUnbind( @observeCurrent[level] )
|
||||||
|
|
|
@ -4,81 +4,79 @@ class App.Event
|
||||||
@init: ->
|
@init: ->
|
||||||
_instance = new _Singleton
|
_instance = new _Singleton
|
||||||
|
|
||||||
@bind: (args) ->
|
@bind: ( events, callback, level ) ->
|
||||||
if _instance == undefined
|
if _instance == undefined
|
||||||
_instance ?= new _Singleton
|
_instance ?= new _Singleton
|
||||||
_instance.bind(args)
|
_instance.bind( events, callback, level )
|
||||||
|
|
||||||
@unbind: (args) ->
|
@unbind: ( events, callback, level ) ->
|
||||||
if _instance == undefined
|
if _instance == undefined
|
||||||
_instance ?= new _Singleton
|
_instance ?= new _Singleton
|
||||||
_instance.unbind(args)
|
_instance.unbind( events, callback, level )
|
||||||
|
|
||||||
|
@trigger: ( events, data ) ->
|
||||||
@cleanUpLevel: (level) ->
|
|
||||||
if _instance == undefined
|
if _instance == undefined
|
||||||
_instance ?= new _Singleton
|
_instance ?= new _Singleton
|
||||||
_instance.cleanUpLevel(level)
|
_instance.trigger( events, data )
|
||||||
|
|
||||||
|
@unbindLevel: (level) ->
|
||||||
|
if _instance == undefined
|
||||||
|
_instance ?= new _Singleton
|
||||||
|
_instance.unbindLevel(level)
|
||||||
|
|
||||||
class _Singleton
|
class _Singleton
|
||||||
|
|
||||||
constructor: (args) ->
|
constructor: ->
|
||||||
super
|
|
||||||
@eventCurrent = {}
|
@eventCurrent = {}
|
||||||
|
|
||||||
cleanUpLevel: (level) ->
|
unbindLevel: (level) ->
|
||||||
return if !@eventCurrent[level]
|
return if !@eventCurrent[level]
|
||||||
for event of @eventCurrent[level]
|
for item in @eventCurrent[level]
|
||||||
@_unbind( level, event )
|
@unbind( item.event, item.callback, level )
|
||||||
|
@eventCurrent[level] = []
|
||||||
|
|
||||||
bind: (data) ->
|
bind: ( events, callback, level ) ->
|
||||||
|
|
||||||
if !data.level
|
if !level
|
||||||
Spine.bind( data.event, data.callback )
|
level = '_all'
|
||||||
return
|
|
||||||
|
|
||||||
if !@eventCurrent[ data.level ]
|
if !@eventCurrent[level]
|
||||||
@eventCurrent[ data.level ] = {}
|
@eventCurrent[level] = []
|
||||||
|
|
||||||
# for all events
|
# level boundary events
|
||||||
events = data.event.split(' ')
|
eventList = events.split(' ')
|
||||||
for event in events
|
for event in eventList
|
||||||
|
|
||||||
# unbind
|
|
||||||
@_unbind( data.level, event )
|
|
||||||
|
|
||||||
for event in events
|
|
||||||
|
|
||||||
# remember all events
|
# remember all events
|
||||||
@eventCurrent[ data.level ][ event ] = data
|
@eventCurrent[ level ].push {
|
||||||
|
event: event,
|
||||||
|
callback: callback,
|
||||||
|
}
|
||||||
|
|
||||||
# bind
|
# bind
|
||||||
Spine.bind( event, data.callback )
|
Spine.bind( event, callback )
|
||||||
|
|
||||||
_unbind: ( level, event ) ->
|
unbind: ( events, callback, level ) ->
|
||||||
console.log '_unbind', level, event
|
|
||||||
return if !@eventCurrent[level]
|
|
||||||
|
|
||||||
data = @eventCurrent[ level ][ event ]
|
if !level
|
||||||
return if !data
|
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) ->
|
# remove from
|
||||||
|
@eventCurrent[level] = _.filter( @eventCurrent[level], (item) ->
|
||||||
if !data.level
|
if callback
|
||||||
Spine.unbind( data.event, data.callback )
|
return item if item.event isnt event && item.callback isnt callback
|
||||||
return
|
else
|
||||||
|
return item if item.event isnt event
|
||||||
if !@eventCurrent[ data.level ]
|
)
|
||||||
@eventCurrent[ data.level ] = {}
|
Spine.unbind( event, callback )
|
||||||
|
|
||||||
# for all events
|
|
||||||
events = data.event.split(' ')
|
|
||||||
for event in events
|
|
||||||
|
|
||||||
# unbind
|
|
||||||
@_unbind( data.level, event )
|
|
||||||
|
|
||||||
|
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: ->
|
constructor: ->
|
||||||
super
|
super
|
||||||
@log 'RUN content'#, @
|
@log 'RUN content'
|
||||||
|
|
||||||
for route, callback of Config.Routes
|
for route, callback of Config.Routes
|
||||||
do (route, callback) =>
|
do (route, callback) =>
|
||||||
@route(route, (params) ->
|
@route(route, (params) ->
|
||||||
|
|
||||||
# remove observers for page
|
# remove observers for page
|
||||||
App.Collection.observeCleanUpLevel('page')
|
App.Collection.observeUnbindLevel('page')
|
||||||
|
|
||||||
# remove events for page
|
# remove events for page
|
||||||
App.Event.cleanUpLevel('page')
|
App.Event.unbindLevel('page')
|
||||||
|
|
||||||
# send current controller
|
# send current controller
|
||||||
params_only = {}
|
params_only = {}
|
||||||
|
@ -59,8 +59,8 @@ class App.Content extends Spine.Controller
|
||||||
)
|
)
|
||||||
|
|
||||||
# unbind in controller area
|
# unbind in controller area
|
||||||
@el.unbind()
|
# @el.unbind()
|
||||||
@el.undelegate()
|
# @el.undelegate()
|
||||||
|
|
||||||
# remove waypoints
|
# remove waypoints
|
||||||
$('footer').waypoint('remove')
|
$('footer').waypoint('remove')
|
||||||
|
|
|
@ -138,13 +138,13 @@ class _Singleton extends Spine.Controller
|
||||||
if typeof item['event'] is 'object'
|
if typeof item['event'] is 'object'
|
||||||
for event in item['event']
|
for event in item['event']
|
||||||
console.log( "ws:onmessage event:" + event )
|
console.log( "ws:onmessage event:" + event )
|
||||||
Spine.trigger( event, item['data'] )
|
App.Event.trigger( event, item['data'] )
|
||||||
else
|
else
|
||||||
console.log( "ws:onmessage event:" + item['event'] )
|
console.log( "ws:onmessage event:" + item['event'] )
|
||||||
Spine.trigger( item['event'], item['data'] )
|
App.Event.trigger( item['event'], item['data'] )
|
||||||
|
|
||||||
# bind to send messages
|
# bind to send messages
|
||||||
Spine.bind 'ws:send', (data) =>
|
App.Event.bind 'ws:send', (data) =>
|
||||||
@send(data)
|
@send(data)
|
||||||
|
|
||||||
@ws.onclose = (e) =>
|
@ws.onclose = (e) =>
|
||||||
|
|
Loading…
Reference in a new issue