admin管理员组

文章数量:1278854

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!

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!

Share Improve this question asked Jan 13, 2013 at 0:35 Chris DutrowChris Dutrow 50.4k67 gold badges195 silver badges262 bronze badges 1
  • 1 the bination of fencliff and Hurricanepkt's answers is what you want... use a region to manage the view being displayed within a DOM element, and listen for the "close" event, or use the onClose callback when needed. – Derick Bailey Commented Jan 13, 2013 at 4:35
Add a ment  | 

2 Answers 2

Reset to default 11

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.

The region show function is going to do most of what you are looking for

https://github./marionettejs/backbone.marionette/blob/master/docs/marionette.region.md#basic-use

And look at the on show event later in the page

本文标签: javascriptDetect when a view is removed from the page using Backbonejs or MarionettejsStack Overflow