Best way to fire an event when app loads a page in cordova app singlepage with jquery mobile?

1.9k Views Asked by At

I'm trying to fire and event at the loading of my app: When the app starts, I load the #home page BUT if the user is not cached (I use kinvey), the app should change page to the #login. How can I accomplish that?

I tryied this code at the beginning of myScript.js file, and this is declared at the beginning:

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

I would appreciate suggestions on what event to trigger, because there are many in jquerymobile documentation and I don't know which is the more convenient.

1

There are 1 best solutions below

2
On

try:

$(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
    });
}

Alternatives might include:

$('#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)

If you're using cordova and have enabled the splash page in config you could try:

$(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(); 
        }
    }
});