Event removal capture in View in Backbone js

Is there any way to listen to delete / kill events on a Backbone view.?

I want to do something like below:

$(myBackboneView).on('remove', function () {
    // do some processing
});

or

$(myBackboneView).on('destroy', function () {
    // do some processing
});

Thanks in advance.:)

+4
source share
3 answers

You can try to override the method View.remove::

Backbone.View.extend({
    remove: function(){
        // Your processing code here

        Backbone.View.prototype.remove.apply(this, arguments);
    };
});
+5
source

I tried the following and this works for me:

$(myBackboneView.el).on('remove', function () {
    // do some processing
});

Is this a good approach? Or is there something even better than this?

+1
source

, "" remove().

BaseView = Backbone.View.extend({
    remove: function () {
        this.trigger('remove', this)
        return Backbone.View.prototype.remove.apply(this, arguments)
    }
})

, :

this.listenTo(otherView, 'remove', func)
0

Source: https://habr.com/ru/post/1523846/


All Articles