【问题标题】:jquery mobile cancel pagebeforeshow eventjquery mobile 取消 pagebeforeshow 事件
【发布时间】:2012-03-16 17:35:05
【问题描述】:

在 jquery mobile 中,有没有办法在 pagebeforeshow 事件中取消页面显示?我在 pagebeforeshow 有一些权限检查代码,如果未满足权限,我想指示用户登录页面。

$("secretpage").live("pagebeforeshow", function () {
  if (permissionNotMet()) {
    stopShowingPage()         //    <---- how?
    $.mobile.changePage("#signin")
  }
})

【问题讨论】:

  • 问得好,我给它搞了几分钟,在转换到重定向页面之前,我无法取消初始页面转换。
  • 我同意我应该将代码放在登录步骤中。我只是认为在实际页面上进行权限检查也是明智之举..
  • pagebeforeload 可以取消,因此如果您检查用户是否对pagebeforeload 具有正确的权限,您可以取消页面加载并将用户重定向到登录页面。我很确定这个事件只会在导航到外部页面时触发,但我不确定。

标签: ajax jquery jquery-mobile


【解决方案1】:

这并不完全符合您的要求,但您可以重新设计您的代码流程,让自己更轻松。

自行处理登录表单的提交,然后根据服务器响应将用户引导至正确的页面:

$(document).delegate('#login', 'pageinit', function () {
    function loginSuccess (serverResponse) {

        $.mobile.hidePageLoadingMsg();

        //here you check the `serverResponse` and direct the user where they need to go
    }
    function loginError (jqXHR, textStatus, errorThrown) {
        console.log(jqXHR);
        console.log(textStatus);
        console.log(errorThrown);
    }
    $(this).find('form').bind('submit', function () {

        $.mobile.showPageLoadingMsg();

        $.ajax({ url : '<url>', data : $(this).serialize(), success : loginSuccess, error : loginError });
        return false;
    });
});

您还需要将data-ajax="false" 作为属性添加到&lt;form&gt; 标记,以便jQuery Mobile 不会为您处理提交。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-14
    • 2014-09-20
    • 1970-01-01
    相关资源
    最近更新 更多