【问题标题】:Confirm before: close the current page, change the url, leave the current 'page'之前确认:关闭当前页面,更改 url,离开当前“页面”
【发布时间】:2013-06-05 23:00:33
【问题描述】:

我正在使用 knockoutjs 和 sammyjs 处理具有多个级别的页面。
例如主页 / LEVEL1 / LEVEL2 / 当前 我已经完成了确认功能:

我怎样才能做到这一点:当人们想要离开当前页面时,将出现确认消息以确保他们确实想要离开此页面(转到 HOME、LEVEL1、LEVEL2、.. 或实际关闭选项卡,更改url) 与否。

【问题讨论】:

    标签: knockout.js sammy.js


    【解决方案1】:

    var app = $.sammy(function() {...}); 内添加以下内容:

    // Will run at #/route but not at #/
    this.before('#/route', function() {
        if(!window.confirm('Are you sure you want to leave this page?')) {
            return false;
        }
    });
    
    // will run at #/ but not at #/route
    this.before({except: {path: '#/route'}}, function() {
        if(!window.confirm('Are you sure you want to leave the home page?')) {
            return false;
        }
    });
    

    来源:Sammy.Application before ( options, callback )

    【讨论】:

    • 感谢您的回答。我知道这个功能,看起来差不多完成了,但是 1. 一旦你点击取消,页面仍然存在,但 url 改变了。 2. 我想要一条确认消息,显示只离开该特定页面。
    • 如果您使用点击绑定(jQuery、Knockout.js 等)而不是锚标记,则 URL 不会更改。然后在点击绑定中,如果确认评估为真,则更改 URL。所以把上面的代码放在你的绑定中,而不是在 Sammy 中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-27
    • 2012-10-02
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    • 1970-01-01
    相关资源
    最近更新 更多