0

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.

Federico Picci
  • 1,105
  • 10
  • 17

1 Answers1

1

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(); 
        }
    }
});
Thierry Blais
  • 2,848
  • 22
  • 41
  • Thanks but none of the above work. kinvey takes a while to connect, so I always get a "kinvey is not defined". Now I'm trying to keep trace of the user access via local storage, it should be faster and safer because it doesn't need a connection, and I will only need cordova to load. – Federico Picci May 20 '14 at 20:32
  • I solved the problem... I made first page #login, in my login function I added on success callback a request to store the username, and a script (onpagecreate, #login) that checks the existance of that value. Everything, works fine. If I press the backButton on my phone I exit the app, as it should be. Thank you anyway for your time. – Federico Picci May 20 '14 at 23:48