Skip to content

Instantly share code, notes, and snippets.

@michikono
Created January 12, 2012 00:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michikono/1597793 to your computer and use it in GitHub Desktop.
Save michikono/1597793 to your computer and use it in GitHub Desktop.
# centralized global_dispatcher object added to all Backbone Collection, Model, View, and Router classes
(->
return if this.isExtended
# attaching the Events object to the dispatcher variable
dispatcher = _.extend({}, Backbone.Events, cid: "dispatcher")
_.each [ Backbone.Collection::, Backbone.Model::, Backbone.View::, Backbone.Router:: ], (proto) ->
# attaching a global dispatcher instance
_.extend proto, global_dispatcher: dispatcher
)()
# See http://www.michikono.com/2012/01/11/adding-a-centralized-event-dispatcher-on-backbone-js/ for an explanation
class Test extends sBackbone.Model
initialize: () ->
this.global_dispatcher.bind('model_custom_action', () ->
console.log('GLOBAL TRIGGERED: test model custom action')
)
this.bind('model_custom_action', () ->
console.log('TRIGGERED: test model custom action')
)
trigger_stuff: () ->
console.log('triggering model')
this.trigger('model_custom_action')
this.global_dispatcher.trigger('model_custom_action')
class TestCollection extends sBackbone.Collection
model: Test
initialize: () ->
this.global_dispatcher.bind('collection_custom_action', () ->
console.log('GLOBAL TRIGGERED: test collection custom action')
)
this.bind('collection_custom_action', () ->
console.log('TRIGGERED: test collection custom action')
)
this.global_dispatcher.bind('model_custom_action', () ->
console.log('GLOBAL TRIGGERED inside Collection: test model custom action')
)
this.bind('model_custom_action', () ->
console.log('TRIGGERED inside Collection: test model custom action')
)
trigger_stuff: () ->
console.log('triggering collection')
this.trigger('collection_custom_action')
this.global_dispatcher.trigger('collection_custom_action')
# Trigger both the local and global event bindings for the collection
# Outputs:
# triggering collection
# TRIGGERED: test collection custom action
# GLOBAL TRIGGERED: test collection custom action
collection = new TestCollection()
# adding an empty, but fully valid Model instance
collection.add()
collection.trigger_stuff()
# Trigger both the local and global event bindings for the model
# Outputs:
# triggering model
# TRIGGERED: test model custom action
# TRIGGERED inside Collection: test model custom action
# GLOBAL TRIGGERED inside Collection: test model custom action
# GLOBAL TRIGGERED: test model custom action
collection = new TestCollection()
# adding an empty, but fully valid Model instance
collection.add()
# getting the just-added instance
instance = collection.at(0)
# trigger an event on the Model
instance.trigger_stuff()
# Trigger event bindings for a model not in a collection
# Outputs:
# triggering model
# TRIGGERED: test model custom action
# GLOBAL TRIGGERED: test model custom action
instance = new Test
instance.trigger_stuff()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment