Question

I am using the event aggregator pattern by Derick Bailey where he has illustrated the pattern in which an object manages the raising of events and the subscribers for those events.

It was all working fine where I was triggering events in one view and subscribing to them in other. The problem came when two or more views subscribe to an event and then at the time of discarding a view, one of the view unsubscribes from the event. This causes all the other views to be unsubscribed from the event as well.

Is there some workaround for this?

Update

Here is the a little bit of code that I'm using in my view:

var EventAggregator = _.extend({}, Backbone.Events);
new MyView({
    collection: MyCollection,
    eventagg: EventAggregator
});
MyView = Backbone.View.extend({
    initialize: function() {
        _.bindAll(this, 'render', 'close', 'actionFnc');
        this.childviews = [];
        this.options.eventagg.bind('evt:action', this.actionFnc);
        this.render();
    },
    render: function() {
    },
    close: function() {
        _(this.childViews).each(function(childview) {
            childview.close();
        });
        $(this.el).empty();
        this.options.eventagg.unbind('evt:action');
    },
    actionFnc: function() {
        // do something over here
    }
});
Was it helpful?

Solution

change the following line:

this.options.eventagg.unbind('evt:action');

to

this.options.eventagg.unbind('evt:action', this.actionFnc);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top