使用jQuery Mobile和$(document).ready与PhoneGap Build和document.addEventListener(" deviceready")

时间:2014-10-29 21:09:44

标签: javascript jquery jquery-mobile cordova phonegap-build

我已经完成了与此相关的两个问题的解决方案 - JQuery document.ready vs Phonegap devicereadyCorrect way of using JQuery-Mobile/Phonegap together? 我仍然没有接近解决方案。

我们的问题在于PhoneGap Build插件。我们正在使用Device插件将特定的UI组件位置和大小定位到iPhone 4和5.这样做我们需要调用事件监听器,使用Device插件获取设备模型,设置变量(iOSversion = true)然后在构建UI时使用该变量。

我们无法让它发挥作用。

我们正在使用jQuery Mobile,所以一切都在

 $(document).ready(function(

这有

document.addEventListener("deviceready", false); 

,以及我们的其他脚本和UI初始化脚本。 jQM是模板,但我们正在构建基于JSON数据的内容和UI组件。直到这一刻,一切都很好。

问题是UI初始化脚本似乎是在事件监听器完成它所做的任何事情之前开始的,所以当UI脚本运行时,iOSversion永远不会真实。

是的,我们可以从onDeviceReady中调用它:

 document.addEventListener("deviceready", onDeviceReady, false);

然后我们无法在浏览器中测试。

我确信有一些简单的解决方案可以让我失踪,但我们已经有一天了,这是最后的障碍,并且每个人都非常沮丧。

1 个答案:

答案 0 :(得分:3)

我得到了答案。

window.cordova在Phonegap Build中公开。所以我检查了这个的存在,在jQuery $(document).ready就像这样:

if ( !!window.cordova ) {
 // phonegap script has loaded so have our 
 // initializeApplication called when the device is ready
 document.addEventListener("deviceready", 
            initializeApplication, false);
}
else {
 // running in browser without phonegap so 
 // manually call initializeApplication
 initializeApplication();
}
function initializeApplication() {
 // do everything here to initialize the
 // application and its UI
}
相关问题