2012-11-08 29 views
0

我正在写的PhoneGap/jQuery Mobile的应用程序,并有一个问题,我似乎无法解决。

当应用程序负载(设备准备就绪,jqm_mobile_init)火灾和应用程序创建/打开数据库,如果用户在(只是一个数据库中的标志)签署的支票。如果是这样,应用程序调用$ .mobile.changePage(“#home”,{transition:“none”});将它们重定向到“主页”页面。

然后在“家”网页pageshow事件我抓住信息的负载从数据库中并将它添加到主页中的列表视图。

然而,这种运行(与$ .mobile.changePage事件)的pageshow事件不触发第一次(所以没有我的数据被追加到列表视图)。如果我浏览应用程序,然后访问该页面,数据显示正常。只有在使用$ .mobile.changePage更改为主页时才会发生这种情况。

如何在$ .mobile.changePage上激发pageshow()触发器?还是有另一种方法来做到这一点?

继承人我的代码:

/************************************************ 
Try to create/open the DB, if not catch the error 
***********************************************/ 
try { 
    if (!window.openDatabase) { 
     alert('not supported'); 
    } else { 
     var shortName = 'test'; 
     var version = '1.0'; 
     var displayName = 'test Database'; 
     var maxSize = 200000; // in bytes 
     // database instance in db. 
     var db = openDatabase(shortName, version, displayName, maxSize); 

     // Create tables 
     createTables(db); 

     // Check if there is a signedin user 
     isUserSignedInQuery(db); 

    } 
} catch(e) { 
    // Error handling code goes here. 
    if (e == 2) { 
     // Version number mismatch. 
     alert("Invalid database version."); 
    } else { 
     alert("Unknown error "+e+"."); 
    } 
    return; 
} 

// Universal null/blank data handler 
function nullDataHandler(transaction, results) { } 

// Universal error callback 
function errorHandler(error) { 
    //alert("Error processing SQL: " +error.message+ " Error Code: " +error.code); 
} 

// Create tables if dont already exist 
function createTables(db) { 
    db.transaction(
     function (transaction) { 

      // create tables 
    } 
    ); 
} 

/********************************************************************************************** 
Check if there is a signed in user, if so redirect to listings page, if not display login page 
**********************************************************************************************/ 

function isUserSignedInQuery(db) { 
    db.transaction(
     function (transaction) { 
      transaction.executeSql("SELECT * FROM USERS WHERE signedIn=1;", 
      [], // array of values for the ? placeholders 
      isUserSignedInDataHandler, errorHandler); 
     } 
    ); 
} 

function isUserSignedInDataHandler(transaction, results) { 
    // Handle the results 
    if (results.rows.length > 0) { 
     //console.log("someones logged in!"); 

     // Assign signed in user to global var 
     console.log("logged in user = " + results.rows.item(0).id); 
     window.currentSignedInUserId = results.rows.item(0).id; 

     $.mobile.changePage("#home", { transition: "none"}); 
    } else { 
     $.mobile.changePage("#login", { transition: "none"}); 
    } 
} 


/********************************************************************************************** 
Sign in page: 
**********************************************************************************************/ 

function doesSigningInUserAlreadyExistQuery(db) { 
    db.transaction(
     function (transaction) { 
      transaction.executeSql("SELECT * FROM USERS WHERE username='"+usernameValue+"' ORDER BY id LIMIT 0,1;", 
      [], // array of values for the ? placeholders 
      doesSigningInUserAlreadyExistDataHandler, errorHandler); 
     } 
    ); 
} 

function doesSigningInUserAlreadyExistDataHandler(transaction, results) { 

    // User exists, sign them in. 
    if (results.rows.length > 0) { 

     //console.log("user exists"); 

     // Find number of rows 
     var len = results.rows.length; 
     //console.log(len); 

     for (var i=0; i<len; i++){ 
      //console.log(results.rows.item(i)); 

      db.transaction(
       function (transaction) { 
        transaction.executeSql('UPDATE USERS SET signedIn = 1 WHERE username="'+usernameValue+'"'); 
       }    
      ); 

      // Assign signed in user to global var 
      window.currentSignedInUserId = results.rows.item(0).id; 

      // Redirect to home/listings page 
      $.mobile.changePage("#home", { transition: "slidefade"}); 
     } 

    // User is new, create them and sign them in 
    } else { 

     db.transaction(
      function (transaction) { 
       transaction.executeSql('INSERT INTO USERS (username, password, userId, defaultHandler, autoSync, updateCaseTypes' 
       +', updateHistorical, updateFavorite, signedIn) ' 
       +'VALUES ("'+usernameValue+'", "eclipse", "userid321", "Another User", 1, 1, 1, 1, 1);', [], 
       function (transaction, resultSet) { 
        if (!resultSet.rowsAffected) { 
         // Previous insert failed. 
         alert('No rows affected!'); 
         return false; 
        } 
        alert('insert ID was '+resultSet.insertId); 

        //Assign signed in user to global var 
        window.currentSignedInUserId = resultSet.insertId; 
       }); 
      }    
     ); 

     // Redirect to home/listings page 
     $.mobile.changePage("#home", { 
      reloadPage: true, 
      transition: "slidefade"}); 

    } 
} 

$('#login').live('pageshow', function(event) { 

    console.log(window.currentSignedInUserId); // This is empty - global var not working 

    // Should this be tap??????? Find out. ----------- 
    $('a#signIn').click(function() { 

     // Get values of all fields & buld vars 
     var username = $('#login-username'); 
     var password = $('#login-password'); 

     // Check if fields are empty 
     if(!username.val()) { 
       username.addClass('empty'); 
       $('label.login-username').addClass('blank'); 
      } 
      if(!password.val()) { 
       password.addClass('empty'); 
       $('label.login-password').addClass('blank'); 
      } 

      // If both not empty, check if user exists, if so sql update if not sql insert 
      if (username.val() && password.val()) { 

       // Get username 
       usernameValue = username.val(); 

       // Run function 
       doesSigningInUserAlreadyExistQuery(db); 

      } 

    }); 
}); 


$('#home').live('pageshow', function(event) { 

    console.log("Page show fired on recordings page"); 

    db.transaction(getRecordingsQuery, getRecordingsDataHandler, errorHandler); 

      // get stuff, loop through it and append 

     // Refresh the list to add JQM styles etc 
     $('#recordings-list').listview('refresh'); 

    } 

}); 

回答

1

我已经成功地解决这个问题,它不是一个真正的正确修复,但它工作在画面闪烁而屏幕刷新的代价。

如果有帮助的人,我说allowSamePageTransitions:真的,解决问题(在闪烁的代价)。

+0

太棒了!...并且不要接受我们自己的答案;-) – Taifun

0

您应该使用on(),而不是live()live()已被弃用。
你有没有试过把它放在beforepageshow而不是pageshow?这似乎是一个更好的地方进行数据收集/动态页面元素生成。

+0

感谢您的回复,我试过(没有区别),我试过beforepageshow(没有区别)似乎使它的工作,但不是理想的唯一的事情是强制刷新加载: $。 mobile.changePage( “#home”,{ reloadPage:真实, 这不是理想的,虽然作为页面刷新明显 - 任何其他的想法 –

+0

尝试'$(文件)。在( 'pageshow',“#home ',function(){...});'而不是。 – Marcus

+0

不是,它完全打破了(当我浏览并通过链接访问它时,甚至不会工作)是否可以用于范围界定?包裹在这 var startApp = function(){ –

相关问题