2013-03-27 39 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语句更加紧凑和可读性。就像你打电话给你的错误回调一样,它可以在你的函数中(在它前面)或者一个完全独立的函数。