0

我正在尝试在加载我的应用程序时触发和事件:当应用程序启动时,我会加载#home page 但是如果用户没有被缓存(我使用 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 文档中有很多,我不知道哪个更方便。

4

1 回答 1

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(); 
        }
    }
});
于 2014-05-16T18:57:27.497 回答