prevent导航到另一种观点认为,如果内容未保存 [英] Prevent navigating to another view if contents are unsaved

查看:88
本文介绍了prevent导航到另一种观点认为,如果内容未保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个显示许多形式向用户Backbone.js的应用程式。我们要的是很简单的:如果用户进入到另一个页面而不保存填写的表单,我们要显示一个确认对话框

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 to display a confirmation dialog.

在经典的形式,这是很容易的,只需要实现window.onbeforeunload(或jQuerysh $(窗口)。在('beforeunload'))。但是骨干网的应用程序只能有一种观点认为,通常。我尝试了一下使用onHashChange,但在返回该回调虚假不从仍然要另一种观点认为没有prevent骨干。

In classic forms this is easy enough, just implement window.onbeforeunload (or $(window).on('beforeunload') in jQuerysh). But backbone apps only have one view, typically. I tried a bit using onHashChange, but returning false in that callback does not prevent Backbone from still going to the other view.

指针是AP preciated。搜索interwebs没有发现我任何有效的响应。

Pointers are appreciated. Searching the interwebs didn't find me any valid response.

推荐答案

我会避免与周围的骨干黑客。你可以通过替换,你通常会启动 Backbone.history 的东西,如

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);
        }
    });
}

您当然需要的东西,有意义的替换 changesAreSaved 并添加您拥有的任何其它登录有关处理环节。

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

这篇关于prevent导航到另一种观点认为,如果内容未保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆