Search code examples
backbone.jsmarionette

Prevent Marionette view from close onBeforeClose


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?


Solution

  • 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