Prevent navigating to another view if contents are unsaved

前端 未结 6 1603
无人共我
无人共我 2021-02-05 20:24

We have a backbone.js app that displays a number of forms to the user. What we want is very simple: if the user goes to another page without saving the filled-in form, we want t

6条回答
  •  无人及你
    2021-02-05 20:50

    Since version 1.2.0 you can override method Router.execute and return false to cancel routing, like this:

    execute: function(callback, args, name) {
        if (!changesAreSaved) {
            // tip: .confirm returns false if "cancel" pressed
            return window.confirm("You sure have some unsaved "
              + "work here, you want to abandon it?");
        }
    
        // this is the default part of "execute" - running the router action
        if (callback)
            callback.apply(this, args);
    }
    

提交回复
热议问题