Improved event bindings.

This commit is contained in:
Martin Edenhofer 2012-10-25 18:42:41 +02:00
parent a0dbc58997
commit 18d04ea16f
9 changed files with 130 additions and 35 deletions

View file

@ -20,12 +20,6 @@ class App.Controller extends Spine.Controller
@delay( a, 0 ) @delay( a, 0 )
reBind: (name, callback) =>
Spine.one name, (data) =>
@log 'rebind', name, data
callback(data)
@reBind(name, callback)
isRole: (name) -> isRole: (name) ->
return false if !window.Session.roles return false if !window.Session.roles
for role in window.Session.roles for role in window.Session.roles

View file

@ -12,7 +12,12 @@ class App.DashboardActivityStream extends App.Controller
# @interval( @fetch, 1400000, 'dashboard_activity_stream' ) # @interval( @fetch, 1400000, 'dashboard_activity_stream' )
@fetch() @fetch()
@reBind( 'activity_stream_rebuild', @load ) # bind to rebuild view event
App.Event.bind(
level: 'page'
event: 'activity_stream_rebuild'
callback: @load
)
fetch: => fetch: =>

View file

@ -4,9 +4,12 @@ class App.DashboardRss extends App.Controller
constructor: -> constructor: ->
super super
# bind to rebuild view event
# bind to new events App.Event.bind(
@reBind( 'rss_rebuild', @load ) level: 'page'
event: 'rss_rebuild'
callback: @load
)
# refresh list ever 600 sec. # refresh list ever 600 sec.
@fetch() @fetch()

View file

@ -14,8 +14,12 @@ class App.DashboardTicket extends App.Controller
# set new key # set new key
@key = 'ticket_overview_' + @view @key = 'ticket_overview_' + @view
# bind new events # bind to rebuild view event
@reBind('ticket_overview_rebuild', @fetch ) App.Event.bind(
level: 'page'
event: 'ticket_overview_rebuild'
callback: @fetch
)
# render # render
@fetch() @fetch()

View file

@ -23,14 +23,15 @@ class Index extends App.Controller
@meta = {} @meta = {}
@bulk = {} @bulk = {}
# set controller to active
Config['ActiveController'] = '#ticket_overview_' + @view
# set new key # set new key
@key = 'ticket_overview_' + @view @key = 'ticket_overview_' + @view
# bind new events # bind to rebuild view event
@reBind( 'ticket_overview_rebuild', @fetch ) App.Event.bind(
level: 'page'
event: 'ticket_overview_rebuild'
callback: @fetch
)
# render # render
@fetch() @fetch()
@ -45,20 +46,6 @@ class Index extends App.Controller
@ticket_list = cache.ticket_list @ticket_list = cache.ticket_list
@load(cache) @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) => load: (data) =>
# get meta data # get meta data
@ -75,6 +62,19 @@ class Index extends App.Controller
@log 'refetch...', record @log 'refetch...', record
@fetch() @fetch()
# # bind render after a change is done
# App.Collection.observe(
# level: 'page',
# collections: [
# {
# collection: @genericObject,
# event: 'refresh change',
# callback: @render,
# },
# ],
# )
@ticket_list_show = [] @ticket_list_show = []
for ticket_id in @ticket_list for ticket_id in @ticket_list
@ticket_list_show.push App.Collection.find( 'Ticket', ticket_id ) @ticket_list_show.push App.Collection.find( 'Ticket', ticket_id )
@ -90,8 +90,6 @@ class Index extends App.Controller
render: -> render: ->
return if Config['ActiveController'] isnt '#ticket_overview_' + @view
# set page title # set page title
@title @overview.meta.name @title @overview.meta.name

View file

@ -30,6 +30,7 @@
#= require ./lib/i18n.js.coffee #= require ./lib/i18n.js.coffee
#= require ./lib/store.js.coffee #= require ./lib/store.js.coffee
#= require ./lib/collection.js.coffee #= require ./lib/collection.js.coffee
#= require ./lib/event.js.coffee
#= require_tree ./models #= require_tree ./models
#= require_tree ./controllers #= require_tree ./controllers
#= require_tree ./views #= require_tree ./views

View file

@ -334,11 +334,13 @@ class _Singleton
for observe in data.collections for observe in data.collections
events = observe.event.split(' ') events = observe.event.split(' ')
for event in events for event in events
if App[ observe.collection ]
App[ observe.collection ].bind( event, observe.callback ) App[ observe.collection ].bind( event, observe.callback )
_observeUnbind: (observers) -> _observeUnbind: (observers) ->
for observe in observers for observe in observers
events = observe.event.split(' ') events = observe.event.split(' ')
for event in events for event in events
if App[ observe.collection ]
App[ observe.collection ].unbind( event, observe.callback ) App[ observe.collection ].unbind( event, observe.callback )

View file

@ -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 )

View file

@ -43,11 +43,15 @@ class App.Content extends Spine.Controller
# remove observers for page # remove observers for page
App.Collection.observeCleanUpLevel('page') App.Collection.observeCleanUpLevel('page')
# remove events for page
App.Event.cleanUpLevel('page')
# send current controller # send current controller
params_only = {} params_only = {}
for i of params for i of params
if typeof params[i] isnt 'object' if typeof params[i] isnt 'object'
params_only[i] = params[i] params_only[i] = params[i]
App.WebSocket.send( App.WebSocket.send(
action: 'active_controller', action: 'active_controller',
controller: route, controller: route,