我正在使用 jquery mobile 和 phonegap (Apache Cordova) 构建一个移动应用程序,问题是首先我需要在决定首先加载哪个页面之前进行数据库查询,如果它是“登录”页面或“主页”。
根据 phonegap 文档,我需要绑定“deviceready”事件以了解设备何时准备就绪,然后进行数据库查询。
document.addEventListener("deviceready", onDeviceReady, false);
如果没有创建名为“onDeviceReady”的函数,则创建数据库,然后对名为“users”的表进行查询,如果有一个或多个用户,我不想显示名为“main.html”的页面,否则为页面命名为“login.html”。
function onDeviceReady() {
var db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
db.transaction(populateDB, errorCB, successCB);
}
基本上,问题是在执行此函数时会加载第一页,因为在调用“onDeviceReady”函数之前执行了以下代码:
$(document).bind( "mobileinit", function(){
$.mobile.listview.prototype.options.filterPlaceholder = "Buscar...";
//-> patch for phonegap
$.mobile.allowCrossDomainPages = true;
$.mobile.page.prototype.options.backBtnText = "atrás";
});
$( document ).bind( "pagebeforeload", function( event, data ){
// here i think i would say to phone gap not to load any page until i make the db queries
//i think i can't make the DB queries here because the "pagebeforeload" is launched before the "deviceready" function
});
如果第一页的代码按照 DOM 的 ASC 顺序加载本页:
<div data-role="page" id="page-init">
<div data-role="header" data-theme="c" data-position="fixed">
<h1>Init page</h1>
</div><!-- /header -->
</div>
如果我将页面更改为“main.html”,$.mobile.changePage("main.html");
一旦我检查“users”表上是否有一个或多个用户记录,则首先加载“page-init”页面,然后加载“main.html”,然后我不想要这个,因为用户可以看到一种闪光。我只想在检查“用户”表后决定首先显示哪个页面。