Prevent Marionette view from close onBeforeClose

2019-08-12 02:40发布

问题:

I'm trying to prevent the user from making an edit to a model via form and easily navigating away from that change. Think Gmail when you've started composing a message.

Someone has posted an issue on GitHub that describes the same situation. https://github.com/marionettejs/backbone.marionette/issues/186

This issue was closed because people suggest you should detect changes before attempting to close a view.

But the problem with this approach is there there are several ways a view can be closed. Back button, clicking a new nav item, cancel button on the view.

Since there is already an onBeforeCLose callback you would think you could cancel the close from here. Is this not at all possible?

回答1:

What you're asking for is built in to the onBeforeClose method now. At the time that ticket #186 was written, the infrastructure wasn't in place for this. Things changed, and it suddenly became a one-liner to implement this, so I did :)

https://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.view.md#view-onbeforeclose


MyView = Marionette.View.extend({

  onBeforeClose: function(){
    // prevent the view from being closed
    return false;
  }

});

var v = new MyView();

v.close(); // view will remain open


回答2:

Unfortunately, at the moment of writing this reply, onBeforeClose does not prevent region's content from being closed. There is an open issue https://github.com/marionettejs/backbone.marionette/issues/703. You can vote for this issue being fixed, by adding a comment.