2013-03-27 37 views
0

我創建移動web應用程序使用html5和javascript.I m有兩個JavaScript文件。 AttributesDatabase.js和AttributeView.js.From AttributeView.js即時調用一個函數從AttributeDatabase.js執行一個選擇查詢。現在查詢結果應該去AtttributeView.js.But Websql事務是異步調用,這就是它沒有返回正確的結果。有什麼辦法來處理websql的結果。 如果有任何方法,請幫忙嗎?如何在html 5中處理websql的結果集?

編輯

AttributeView.js 

var AttributeDAOObj = new AttributeDAO(); 

AttributeDAOObj.GetAttributeList(); 
alert(AttributeDAOObj.GetAttributeList()); //This alert is coming as undefined. 

AttributeDAO.js 
this.GetAttributeList = function() { 
    var baseDAOObj = new BaseDAO(); 
    var query = "SELECT AttributeName FROM LOGS"; 
    // this.Successcalbackfromsrc = this.myInstance.Successcalback; 
    var parm = { 'query': query, 'Successcalback': this.myInstance.Successcalback }; 
    baseDAOObj.executeSql(parm); 
} 

//To Create database and execute sql queries. 
function BaseDAO() { 
this.myInstance = this; 
//Creating database 
this.GetMobileWebDB = function() { 
    if (dbName == null) { 
     var dbName = 'ABC'; 
    } 
    var objMobileWebDB = window.openDatabase(dbName, "1.0", dbName, 5 * 1024 * 1024); 
    return objMobileWebDB; 
} 

//Executing queries and getting result 
this.executeSql = function (query) { 
    var objMobileWebDB = this.myInstance.GetMobileWebDB(); 
    objMobileWebDB.transaction(function (transaction) { 
//In this transaction i m returning the result.The result value is coming. 
     transaction.executeSql(query, [], function (transaction, result) { return result; }, this.Errorclback); 
    }); 
} 

}

+0

如果您發佈了一些代碼,它將更容易回答您的問題 – jugg1es 2013-03-27 17:24:11

+1

您不返回異步操作的結果,您在結果到達時調用回調。 – DCoder 2013-03-27 19:26:15

回答

0

的問題是在你更迭(在你的問題,通過DCoder規定的評論等)回調

function (transaction, result) { return result; } 

這是回訪去哪兒?

因此,這是如何做到這一點(或至少單程)

比如,你可以這樣做:

function (transaction,result){ 
    console.log("yes, I have some result, but this doesn't say anything, empty result gives also a result"); 
    // so check if there is a result: 
    if (result != null && result.rows != null) { 
     if (result.rows.length == 0) { 
      // do something if there is no result 
     }else{ 
      for (var i = 0; i < result.rows.length; i++) { 
       var row = result.rows.item(i); 
       var id = result.rows.item(i).id; //supposing there is an id in your result 
       console.log('Yeah! row id = '+id); 
      } 
     } 
    }else{ 
     // do something if there is no result 
    } 
}; 

注意上面的代碼可以更緊湊,但是這是怎麼理解呢更好。

另一種方法是把這個函數是一段獨立的代碼,讓你保持sql語句更加緊湊和可讀性。就像你打電話給你的錯誤回調一樣,它可以在你的函數中(在它前面)或者一個完全獨立的函數。