using Backbone.js with Marionette.js (Go Derick Bailey!). Need to detect when a view is removed from the page. Specifically, I'm overwriting it with another view.
Is there an event I can detect of function I can overload to detect when this happens?
Thanks!
Marionette provides the View.onClose method for this purpose:
Backbone.Marionette.ItemView.extend({
onClose: function(){
// custom cleanup or closing code, here
}
});
In vanilla Backbone you can override the View.remove method:
Backbone.View.extend({
remove: function(){
// custom cleanup or closing code, here
// call the base class remove method
Backbone.View.prototype.remove.apply(this, arguments);
}
});
Neither of these methods will work if you are simply clobbering the view's DOM element. If that is your case, the solution is simple: Don't do that. Remove the previous view explicitly before rendering another view in its place.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With