2012-11-08 34 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(){ –

相關問題