jQuery mobile $ .mobile.changePage在iPhone上不能正常工作PhoneGap [英] jQuery mobile $.mobile.changePage not working properly on iPhone in PhoneGap

查看:126
本文介绍了jQuery mobile $ .mobile.changePage在iPhone上不能正常工作PhoneGap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用jQuery mobile为框架开发了一个PhoneGap应用程序,我遇到了一个恼人的问题。我最初在Android上开发它,并且都工作正常。



现在,它应该是跨平台,但是当我把它带到iPhone,它不会过去闪屏。我跟着几个红色herrings,但最终设法在JavaScript中解决这个问题。



这是一个调用 $。mobile.changePage 在程序执行早期导致问题。看起来是一种竞态条件,因为当我注释掉了错误的代码行,它已经过了初始屏幕,然后如果我从Safari开发控制台远程运行语句,它工作正常。为了增加我的沮丧,它甚至在iPad上的Safari浏览器中工作正常,但只是不是在PhoneGap应用程序,这更奇怪。



我遵循一些建议使用setTimeout作为一个解决方法,我从一个jQuery移动github问题页面这里:
https://github.com/jquery/jquery-mobile/issues/3190



这使它工作(谢谢!!)。但是,我做错了什么?我觉得应该有一个更好的解决方案,而不仅仅是为程序执行添加一个延迟。



这里是代码:

  $(document).on('pageinit','#home',onHomePageInit); 

function onHomePageInit(){
$(#home)。on(pageshow,checkTokenAndSync());
}

function checkTokenAndSync(){
//检查存储值
var storedId = window.localStorage.getItem('id');
var tokenValue = window.localStorage.getItem('token');

if(storedId == null || tokenValue == null){
//找不到令牌值,直接用户登录屏幕获取一个

/ /这不工作。需要使用setTimeout如下
// $ .mobile.changePage(#loginDialog);

window.setTimeout(changeToLoginDialog,1000);
}
}

function changeToLoginDialog(){
$ .mobile.changePage(#loginDialog);
}

我要向github问题页面添加注释。 p>

解决方案

如果任何人正在寻找这个问题的快速解决方法。尝试使用window.setTimeout向程序执行添加延迟。请参阅上面的问题代码。


I was having an annoying problem that had me stumped for about two days.

I developed a PhoneGap application using jQuery mobile for the framework. I initially developed it on Android and all worked fine.

Now, it's supposed to be cross-platform but when I brought it over to iPhone, it wouldn't get past the splash screen. I followed a few red herrings but eventually managed to zone in on the problem in JavaScript.

It was a call to $.mobile.changePage early on in the program execution that was causing the problem. Appears to be some kind of race condition because when I commented out the offending line of code, it got past the splash screen and then if I ran the statement remotely from the Safari development console, it worked okay. To add to my frustration, it even worked okay in the Safari browser on the iPad but just not within the PhoneGap app which was even more strange.

I followed some advice to use setTimeout as a workaround which I got from a jQuery mobile github issue page here: https://github.com/jquery/jquery-mobile/issues/3190

This made it work (thankfully!!). However, am I doing something wrong? I feel there should be a better solution than merely adding a delay to the program execution.

Here is the code:

$(document).on('pageinit', '#home', onHomePageInit);     

function onHomePageInit() {
    $("#home").on("pageshow", checkTokenAndSync());
}

function checkTokenAndSync() {
    // check for stored values
    var storedId = window.localStorage.getItem('id');
    var tokenValue = window.localStorage.getItem('token');

    if(storedId == null || tokenValue == null) {
        // no token value found, direct user to login screen to obtain one

        // This doesn't work.  Need to use setTimeout as below
        // $.mobile.changePage("#loginDialog");

        window.setTimeout(changeToLoginDialog, 1000);
    }   
}

function changeToLoginDialog() {
    $.mobile.changePage("#loginDialog");
}

I'm going to add a comment to that github issue page.

解决方案

In case anyone is looking for a quick-fix workaround to this problem. Try using window.setTimeout to add a delay to the program execution. See question above for code.

这篇关于jQuery mobile $ .mobile.changePage在iPhone上不能正常工作PhoneGap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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