0
votes

I've got a Marionette.Layout that manages a bunch of region sub-views that contain information I'd like to save before the user navigates away from the page. I was using onBeforeClose() in the Layout, but the regions seem to be closed and removed before the Layout is. Further, it doesn't seem like returning false in onBeforeClose() can prevent destruction of sub-views either. Any thoughts on whether there is an alternative approach or whether Marionette's onBeforeClose() should be called earlier?

Others have suggested detecting changes before the close happens, but as mentioned here (Prevent Marionette view from close onBeforeClose) it doesn't capture all the close cases.

1

1 Answers

0
votes

I usually end up writing my own close method when I need to change the default behavior. I'll create a mixin if it's going to be reusable. Something like:

close: function() {
    if (this.isClosed){ return; }
    // the following if statement is the only difference between
    // Marionette.Layout.close and my view's version.
    // Everything in the if is original
    if ( myCondition ) {
        this.regionManager.close();
        var args = Array.prototype.slice.apply(arguments);
        Marionette.ItemView.prototype.close.apply(this, args);
    }
}

It's not necessarily what you were looking for, but it is an option. onBeforeClose is called from Marionette.View.close, which in turn is called from ItemView.close. This is the only way I've found to get the control I'm looking for.