应用程序使用jquery mobile在cordova app单页中加载页面时触发事件的最佳方法是什么?

时间:2014-05-16 14:57:50

标签: javascript jquery-mobile cordova kinvey jquery-mobile-pageshow

我正在尝试在加载我的应用时触发和事件: 当应用程序启动时,我加载#home页面但是如果用户没有被缓存(我使用kinvey),应用程序应该将页面更改为#login。 我怎么能做到这一点?

我在myScript.js文件的开头尝试了这段代码,并在开头声明:

$('#home').on( 'pageshow',function(){
    var user = Kinvey.getActiveUser();
    var promise = Kinvey.User.me({
    success: function(response) {
    $.mobile.changePage('#login'); //should change page here
    });
}

我很感激有关触发什么事件的建议,因为jquerymobile文档中有很多,我不知道哪个更方便。

1 个答案:

答案 0 :(得分:1)

尝试:

$(document).on( 'mobileinit' , function(){ // will fire as soon as jQM is loaded
    var user = Kinvey.getActiveUser();
    var promise = Kinvey.User.me({
    success: function(response) {
    $.mobile.changePage('#login', {transition: 'none'}); //should change page here
    });
}

替代方案可能包括:

$('#home').on( 'pageinit' ,function(){ /*stuff*/ }); //will fire the first time #home is loaded
$('#home').on( 'pagebeforeshow' ,function(){ /*stuff*/ }); //will fire before home is shown (don't do this, it'll check every time)

如果您正在使用cordova并在配置中启用了启动页面,则可以尝试:

$(document).on( 'mobileinit' , function(){ // will fire as soon as jQM is loaded
    var user = Kinvey.getActiveUser();
    var promise = Kinvey.User.me({
    success: function(response) {
        $.mobile.changePage('#login', {transition: 'none'}); //should change page here
        if (navigator.splashscreen /* || 1 */ ){ // so it doesn't crash when you're testing on your browser
            navigator.splashscreen.hide(); 
        }
    }
});