2012-05-29 33 views
1

我使用jQuery Mobile和PhoneGap的(Apache的科尔多瓦)建立一个移动应用程序,问题是,首先我需要一个数据库查询之前,我决定我想先加载,如果是“登录”页面的页面或“主页”。如何决定首先在jquery mobile上加载哪个页面?

基于PhoneGap的文件,我需要绑定的“deviceready”事件,知道什么时候该设备已准备就绪,然后使DB查询上。

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

称为“onDeviceReady”功能创建DB如果它不是创建,然后做一个查询到一个名为“用户”表,如果有一个或多个用户我wan't显示一个名为“页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 
}); 

如果的代码根据ASC顺序DOM此页面加载第一页:

<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");一次我检查,如果有一个或更多的用户记录在“用户”表上,“页面初始化”页面首先加载,然后“main.html”,我不想这样做,因为用户可以选择一种闪光。 我只是想决定一次,我检查了“用户”表,该页面将首先显示。

回答

1

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

4

我在这里学到以下的计算器,但我再也找不到答案,所以我会回答你自己:在你的指数结束

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

在任何JS文件或script index in index:

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你可以把初始化代码别的地方,但加载屏幕效果会更好,因为它是一个分贝的呼叫,我使用的浏览器存储是这样的方式更快,我认为

+1

不能相信这个解决方案从来没有起床投票!同时使用加载页面和此解决方案可以工作,但上述解决方案的问题在于它将使用#标记设置页面。工作,但将网址更改为http://example.com#my-page,这是可以的,除非你想隐藏用户的散列(就像我的情况一样) –

相关问题