Deferred,Jquery移动语句转换为$ .when回调 [英] Deferred, Jquery mobile statements into $.when callback

查看:120
本文介绍了Deferred,Jquery移动语句转换为$ .when回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的应用程式(使用Jquery Mobile 1.4)可以以PhoneGap应用程式执行

I am trying to make my app (which uses Jquery Mobile 1.4) able to run


  • (使用Phonegap Build,而不是Xcode)

  • either as a Phonegap app (using Phonegap Build, not Xcode)

或作为标准浏览器上的纯web应用程序启动我的js文件。

or as a pure webapp on a standard browser by starting my js file like below.

但是没有绑定任何Jquery Mobile事件...

But none of the Jquery Mobile events are bound...

我明白为什么?

var deviceReadyDeferred = $.Deferred();
var jqmReadyDeferred = $.Deferred();

$(document).one("mobileinit", function () {
    console.log('mobileinit just fired');
    jqmReadyDeferred.resolve();
});

document.addEventListener("deviceReady", onDeviceReady, false);
function onDeviceReady() {
    deviceReadyDeferred.resolve();
}

if ( isPhoneGap() ) {
    alert("isPhoneGap yes");
    $.when(deviceReadyDeferred, jqmReadyDeferred).then( EVERYTHING );
} else {
    console.log("NOT Running on PhoneGap!");
    $.when(jqmReadyDeferred).then( EVERYTHING );
}

function EVERYTHING() {

    //all the JQM bindings here:
    $(document).on('pagecontainershow', function (e, ui) {
        //...
    });
    $(document).on('pagecreate','#splash-page', function(){
        //...
    });
    $(document).on('pagecreate','#faq-page', function(){
        //...
    });
    //etc.
}


$ b $ p PS:我使用函数isPhoneGap从在这里,这很好。

推荐答案

它不应该。你的描述是很常见的,我找不到什么错。
注意,你不需要在$(document).ready()函数中声明Deffered对象,只是让它成为。

It should not be. Your description is quite common and I could not find anything wrong. Note that you do not need to declare the Deffered object in $(document).ready() function, just let it be.

在2个地方添加弹出消息:
onDeviceReady:知道deviceready事件是否工作。
一切:知道这两个Deffered对象是否都解决了。

I think you need to add popup message in 2 places: onDeviceReady: to know whether the deviceready event works. EVERYTHING: to know whether these 2 Deffered object are both resolved.

BTW,我不认为isPhoneGap是一个完美的, 有时,你使用Phonegap构建来编译一个应用程序项目,但你忘记添加cordova.js声明,这个功能仍然会告诉你它是一个Phonegap应用程序(这可能是最新的条件为你)。
此函数仅基于链接的网址。但是,对于我的意见,是否deviceready事件是工作是唯一的和最好的方法来判断它是否是一个Phonegap应用程序。

BTW, I do not think isPhoneGap is a perfect one to detect whether an app is a Phonegap one or not. Sometime, you use Phonegap build to compile an app project but you forget to add cordova.js declaration, this function will still tell you "it is a Phonegap app"(This maybe current condition for you). This function is only based on the url of the link. However, as for my opinion, whether deviceready event is working is the only and best way to judge if it is a Phonegap app.

这篇关于Deferred,Jquery移动语句转换为$ .when回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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