1

我正在使用 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”,然后我不想要这个,因为用户可以看到一种闪光。我只想在检查“用户”表后决定首先显示哪个页面。

4

3 回答 3

5

我在stackoverflow上学到了以下内容,但我找不到答案了,所以我会自己回答:

在您的索引末尾:

            $(document).bind("mobileinit", onMobileInit);
            $(document).ready(onPageLoad);

在索引中的任何 JS 文件或脚本标记中:

function onMobileInit() {
    console.log("mobile init");
    $.mobile.autoInitialize = false;
}

function onPageLoad() {// Document.Ready
    console.log("document ready");
    try {
        if(someCondition) {
            document.location.hash = "#profile";
        } else {
            document.location.hash = "#signIn";
        }
    } catch (exception) {

    } finally {
        $.mobile.initializePage();
    }
}

PS您可以将初始化代码放在其他地方,但是加载屏幕会更好,因为它是一个数据库调用,我使用浏览器存储,我认为这要快得多

于 2012-10-09T08:08:45.000 回答
1

听起来这可以通过加载屏幕来解决。只需将您的第一页创建为加载屏幕,然后检查您的数据库并根据数据库结果加载正确的页面。您甚至可以添加 JQM 微调器来告诉您的用户正在发生的事情。

于 2012-05-29T14:51:17.787 回答
0

这个问题很老,但是我可以说,对我来说,只需将您想要的散列分配给 document.location.hash,例如 document.location.hash = "#profile";,而无需移动初始化程序就可以正常工作。您需要在 document.ready() 运行之前执行此操作。

于 2020-02-12T12:05:29.517 回答