<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://devlicio.us/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Mike Nichols - Son Of Nun Technology : Backbone</title><link>http://devlicio.us/blogs/mike_nichols/archive/tags/Backbone/default.aspx</link><description>Tags: Backbone</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>Backbone Event Aggregator extended</title><link>http://devlicio.us/blogs/mike_nichols/archive/2011/08/14/backbone-event-aggregator-extended.aspx</link><pubDate>Mon, 15 Aug 2011 03:21:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:68104</guid><dc:creator>Michael Nichols</dc:creator><slash:comments>2</slash:comments><comments>http://devlicio.us/blogs/mike_nichols/archive/2011/08/14/backbone-event-aggregator-extended.aspx#comments</comments><description>&lt;p&gt;&lt;b&gt;UPDATE : &lt;a target="_blank" href="http://devlicio.us/blogs/mike_nichols/archive/2011/10/20/backbone-events-and-aggregator-update.aspx"&gt;Please use the code found in my follow-up.&lt;/a&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://lostechies.com/derickbailey/2011/07/19/references-routing-and-the-event-aggregator-coordinating-views-in-backbone-js/"&gt;Derick Bailey wrote a great post&lt;/a&gt; decoupling Backbone Views from one another through the use of an Event Aggregator. 
I wanted to use this without passing in the dispatcher into all these Views, Models, etc. To accomplish this I came 
up with the following:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;//namespace our extensions

var BackboneExt = BackboneExt || function() {
    var backbonePrototypes = [
        Backbone.Collection.prototype, 
        Backbone.Model.prototype, 
        Backbone.View.prototype,
        Backbone.Router.prototype
    ];

    var extensions = {

        dispatcher : _.extend({}, Backbone.Events,{cid : &amp;#39;dispatcher&amp;#39;}),
        wrappedEvents : function(localEvents) {
            var local = localEvents || _.extend({},Backbone.Events);
            //note that &amp;#39;this&amp;#39; is the Backbone implementation
            return {            
                dispatcher : BackboneExt.dispatcher,            
                trigger : function() {                                      
                    var args = _.toArray(arguments);                
                    // this._callbacks is an internal collection 
                    // currently backbone could 
                    if( this._callbacks ) {                             
                        local.trigger.apply(this,args);
                    }               
                    BackboneExt.dispatcher.trigger.apply(BackboneExt.dispatcher,args);
                    return this;            
                }

            };

        },
        extendBackbone : function() {
            var self = this;
            if( self.isExtended ) {
                return;
            }
            _.each(backbonePrototypes,function(proto) {
                _.extend(proto, self.autoBinder, self.wrappedEvents());
            });
        }
    };
    return extensions;
}();

BackboneExt.extendBackbone();
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that &lt;code&gt;dispatcher&lt;/code&gt; is now exposed in any of the Backbone components. We just overwrite the default &lt;code&gt;Backbone.Events.trigger&lt;/code&gt;
with our own that composes the local &lt;code&gt;trigger&lt;/code&gt; with the global (dispatcher) &lt;code&gt;trigger&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;&lt;code&gt;trigger&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;This keeps my Backbone components blissfully ignorant of its subscriber(s):&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;// some simple jquery notification component
BackboneExt.dispatcher.bind(&amp;#39;mynamespaced:event&amp;#39;, function(args) {
    $(&amp;#39;#messages&amp;#39;).append(args.message);
});

// some Backbone.Model
var model = new SomeModel({
    // ...
    someMethod : function() {
        this.trigger(&amp;#39;mynamespaced:event&amp;#39;,{ message : &amp;#39;something happened&amp;#39; });
    }
});
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;&lt;code&gt;bind&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;This can work the other way, too, but the Backbone components need to explicitly bind to the exposed dispatcher:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;//inside Backbone.Model initializer
var model = new SomeModel( {
    initialize : function() {
        dispatcher.bind(&amp;#39;someglobalevent:happened&amp;#39;, this.someMethod);
    }
    someMethod : function() { ... }
});

BackboneExt.dispatcher.trigger(&amp;#39;someglobalevent:happened&amp;#39;, &amp;#39;something happened&amp;#39;); //calls &amp;#39;someMethod&amp;#39; in model
&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=68104" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/mike_nichols/archive/tags/Backbone/default.aspx">Backbone</category></item></channel></rss>