0

オブジェクトにイベント アグリゲータがある場合、

eventAggregator: _.extend({}, Backbone.Events),

モーダルビューの場合、基本的にモーダルビューのプレゼンターにモーダルビューのイベントをリッスンさせます。

this.eventAggregator.on('modal:close', function () {
console.log('do some clean up');
});

モーダルビューが消えたら、私は呼び出します

this.unbind(); 

それはすべてのイベントを削除しますか?または、次のようなことをする必要がありますか

this.eventAggregator.off('modal:close');

前もって感謝します!

4

3 に答える 3

2

最近のバックボーンにはlistenTo( http://backbonejs.org/#Events-listenTo ) があるので、次の方法でイベントをサブスクライブできます。

this.listenTo(this.eventAggregator, 'modal:close', function () {
    console.log('do some clean up');
})
this.listenTo(this.model, 'change', this.doSomeStuff);

次に、購読を解除する必要がある場合は、次のように呼び出します。

this.stopListening();

view.removeまたは( http://backbonejs.org/#View-remove ) でビューを削除すると、内部で呼び出さview.stopListeningれます。

于 2013-05-01T07:53:45.603 に答える
1

o.unbind()内のオブジェクトについては何も知りません。使用oすることにバインドされているものについてのみ知っています(AKA )。いいえ、にバインドされているものについては何もしません。次のようにする必要があります。oo.ono.bindthis.unbind()this.eventAggregator

this.eventAggregator.unbind();
this.unbind();

両方のリストをクリアします。

于 2013-05-01T03:49:59.133 に答える
0

イベントのバインド/バインド解除に on/off を使用したい場合は、おそらくこれを行うことができます。

//declare some global object to get the scope.
window.eventAggregator = _.extend({}, Backbone.Events);

バックボーン ビューに表示されます。

var SomeView = Backbone.View.extend({
    el : 'body',
    initialize : function() {

        //pass the context as this object
        eventAggregator.on('modal:close', function(data) {
            console.log('Clean up');
            eventAggregator.off('modal:close', null, this);
        }, this);
    }
});

eventAggregator.off('modal:close', null, this);「この」現在のビューオブジェクトにバインドされたイベントをオフにするのに役立ちます。

于 2013-05-03T10:32:58.823 に答える