Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching remove event on View in Backbone js

Is there any way to listen to remove/destroy event on the Backbone View.?

I want to do some thing like as below:

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

or

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

Thank you in advance. :)

like image 539
Rahul Bhanushali Avatar asked Jan 27 '14 06:01

Rahul Bhanushali


Video Answer


2 Answers

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

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

        Backbone.View.prototype.remove.apply(this, arguments);
    };
});
like image 101
Felix Avatar answered Oct 14 '22 05:10

Felix


I tried the following and it works for me:

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

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

like image 37
Rahul Bhanushali Avatar answered Oct 14 '22 03:10

Rahul Bhanushali