Hitul Mistry
Hitul Mistry

Reputation: 2275

In backbone.js how to fire view unload event?

I have view and i want to fire one event when view unloaded.

Backbone.View.extend({
    initializer : function() { 
        // Constructor
    },
    render : function() {
        // My render logic.
    }
})

Now how to fire one event when above view unloaded.

Upvotes: 1

Views: 1879

Answers (2)

Jacopo Lanzoni
Jacopo Lanzoni

Reputation: 1316

I am not sure you can do that. You may want to find a workaround using

window.addEventListener('beforeunload', myFunction);

Upvotes: 1

Jonathan Naguin
Jonathan Naguin

Reputation: 14796

You can override the remove method from Backbone.View as:

remove: function () {

    this.trigger('view:unload'); //Whatever event name

    Backbone.View.prototype.remove.call(this); //Important
},

It is important to call the original function to delete correctly the view.

Upvotes: 1

Related Questions