Created
January 12, 2012 00:58
-
-
Save michikono/1597793 to your computer and use it in GitHub Desktop.
Backbone Global Dispatcher (article @ http://www.michikono.com/2012/01/11/adding-a-centralized-event-dispatcher-on-backbone-js/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
)() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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