2013-02-08 56 views
0

我正在編寫一個移動應用程序,用於在本地使用Sqlite存儲數據。在過去的4天裏,我一直在試圖弄清楚爲什麼當Jquery和phonegap完全加載時數據庫沒有創建。在sqlite中創建語句不起作用,回調函數不起作用。 deviceready不起作用,但如果檢查sqlite支持它會觸發。示例代碼是別人的代碼,但發生了同樣的事情。有人可以幫幫我嗎?Phonegap deviceready not responding/firing

var jqmReady = $.Deferred(), 
pgReady = $.Deferred(); 

// jqm page is ready 
$(document).bind("pageinit", jqmReady.resolve); 

// phonegap ready 
document.addEventListener("deviceready", pgReady.resolve, false); 

// all ready, throw a custom 'onDeviceready' event 
$.when(jqmReady, pgReady).then(function(){ 
    $(document).trigger("onDeviceready"); 
}); 


function onDeviceReady(){ 
     db.transaction(populateDB, errorCB, successCB); 
    } 

    //create table and insert some record 
    function populateDB(tx) { 
     tx.executeSql('CREATE TABLE IF NOT EXISTS SoccerPlayer (id INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT NOT NULL, Club TEXT NOT NULL)'); 
     tx.executeSql('INSERT INTO SoccerPlayer(Name,Club) VALUES ("Alexandre Pato", "AC Milan")'); 
     tx.executeSql('INSERT INTO SoccerPlayer(Name,Club) VALUES ("Van Persie", "Arsenal")'); 
    } 

    //function will be called when an error occurred 
    function errorCB(err) { 
     alert("Error processing SQL: "+err.code); 
    } 
    //function will be called when process succeed 
    function successCB() { 
     alert("success!"); 
     db.transaction(queryDB,errorCB); 
    } 

回答

0

我沒有在模擬器或物理設備上運行它,但從查看代碼我可以看到一個問題。試試這個,看看是否有幫助:

變化

$.when(jqmReady, pgReady).then(function(){ 
    $(document).trigger("onDeviceready"); 
}); 

$.when(jqmReady, pgReady).then(function(){ 
    onDeviceReady(); 
}); 

的原因,我認爲改變是因爲$(document).trigger("onDeviceready")觸發了 'onDeviceready' 事件。您沒有偵聽器設置來捕獲該事件,並且我假設您想要它執行的操作是調用「onDeviceReady()」函數。

+0

謝謝傑弗瑞,我按照你的指示,它的工作。 – 2013-02-24 00:18:36

相關問題