2

ここでは、オブジェクトがイベントの発生とそれらのイベントのサブスクライバーを管理するパターンを示したDerick Bailey によるイベント アグリゲーター パターンを使用しています。

あるビューでイベントをトリガーし、別のビューでそれらをサブスクライブしていたところ、すべて正常に機能していました。この問題は、2 つ以上のビューがイベントにサブスクライブし、ビューを破棄するときに、ビューの 1 つがイベントのサブスクライブを解除したときに発生しました。これにより、他のすべてのビューもイベントからサブスクライブ解除されます。

これに対する回避策はありますか?

アップデート

これが私のビューで使用しているコードの一部です。

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
    }
});
4

1 に答える 1

4

次の行を変更します。

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

this.options.eventagg.unbind('evt:action', this.actionFnc);
于 2011-11-18T16:25:01.023 に答える