Prevent navigating to another view if contents are unsaved

前端 未结 6 1606
无人共我
无人共我 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 21:02

    I would avoid hacking around with Backbone. You could do this globally for all links by replacing the part where you would normally start Backbone.history with something like

    initRouter: function () {
        Backbone.history.start({ pushState: true });
        $(document).on('click', 'a', function (ev) {
            var href = $(this).attr('href');
            ev.preventDefault();
            if (changesAreSaved) {
                router.navigate(href, true);
            }
        });
    }
    

    You need of course to replace the changesAreSaved with something that makes sense and add whatever other login you have about handling links.

提交回复
热议问题