简体   繁体   English

jquery移动功能在设备准备好之前运行

[英]jquery mobile function running before the device ready

I am building an android application using phonegap, and jQuery Mobile. 我正在使用phonegap和jQuery Mobile构建一个android应用程序。

From phonegap documentation, device ready function needs to be fired first before anything else. 从phonegap文档中,需要首先触发设备就绪功能。

I have no idea why but 我不知道为什么

$(document).on("pageshow", "#keeperList", function(){ 
listAllKeepers();
});

is firing first. 首先开火。

I cannot post whole code, since it is too much. 我不能发布整个代码,因为它太多了。

 <script type="text/javascript" src="js/cordova.js"></script>
 <script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
 <script type="text/javascript" src="js/jquery.mobile-1.3.1.js"></script>
 <script type="text/javascript" src="js/db.js"></script>
 <script type="text/javascript">

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

 function onDeviceReady() {
 alert("PhoneGap is ready!");
 db = window.openDatabase("rentManag", "3.7.11", "Rent Manag", 100000000);
 db.transaction(createTable, errorCB, successCB);
 }
 $(document).on('pageshow', "#keeperList", function () { 
 listAllKeepers();
 });
 </script>

You have to wait for the JQuery Mobile "pagecreate" and Phonegap "deviceready" event if you're using phonegap in combination with JQuery Mobile. 如果你将phonegap与JQuery Mobile结合使用,你必须等待JQuery Mobile“pagecreate”和Phonegap“deviceready”事件。 This ensures that both frameworks are properly loaded. 这可确保正确加载两个框架。 This is how you do it: 这是你如何做到的:

var jqmReady = $.Deferred();
var pgReady = $.Deferred();

// jqm ready
$(document).bind("pagecreate", jqmReady.resolve);

// phonegap ready
document.addEventListener("deviceready", pgReady.resolve, false);
$(document).on('pagecreate',function(event,data)
                 {


                 });
// all ready :)
$.when(jqmReady, pgReady).then(function () {
          listAllKeepers();
                               });

To give an answer on your question, check this code below. 要对您的问题给出答案,请查看下面的代码。 It is a work-around I use myself. 这是我自己使用的解决方法。 It is likely not the best solution, but it gets the job done. 它可能不是最好的解决方案,但它可以完成工作。

What does it do: - On device ready it sets a value to true. 它做什么: - 在设备就绪时,它将值设置为true。 - On page load you access a function that waits on that value being true. - 在页面加载时,您访问一个等待该值为true的函数。 If not, it loops until it is. 如果没有,它会循环直到它。 - This prevents errors that stuff is not being loaded by PhoneGap yet. - 这可以防止PhoneGap尚未加载任何内容的错误。

// device ready document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { // let the function "isDeviceReady" know that the event "deviceready" has been fired window.deviceReady = true; }

// callback function to check if device is ready function isDeviceReady(value, action) { if (window.deviceReady === true) { switch (action) { case "listAllKeepers": listAllKeepers(); break; case "listAllKeepersValue": // if you had a value listAllKeepers(value); break; } } else { window.setTimeout("isDeviceReady(\\"" + value + "\\", \\"" + action + "\\");", 100); } }

// do stuff on page show $(document).on('pagebeforeshow', '#yourpageid', function (event, data) { isDeviceReady('', listAllKeepers); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM