Javascript 回调、返回值和 HTML5 executeSql 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1898178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Callback, return value and HTML5 executeSql function
提问by Tibor
I have a big problem. I know it's about callback, closure but I don't know how to solve the problem. Here is my code
我有一个大问题。我知道这是关于回调,关闭,但我不知道如何解决问题。这是我的代码
$.Model.extend('Article',
{
findAll : function(params, success, error){
var result = []
db.transaction(function(tx) {
tx.executeSql('select * from contents', [],function(tx, rs) {
for(var i=0; i<rs.rows.length; i++) {
var row = rs.rows.item(i)
result[i] = {
id: row['id'],
title: row['title'],
body: row['body']
}
}
})
})
//here result is undefined
alert(result)
return result
}
})
//undefined
var view = Article.findAll
I know that executeSql is asynchronous function, but I don't know how to save and return result of executeSql. I use javascript mvc and HTML offline database.
我知道executeSql是异步函数,但我不知道如何保存和返回executeSql的结果。我使用 javascript mvc 和 HTML 离线数据库。
Thank you for your help
感谢您的帮助
回答by Charlie Brown
The W3C web database spec talks about support for both Asynchronous and Synchronous database operations. (See 4.3 and 4.4)
W3C Web 数据库规范讨论了对异步和同步数据库操作的支持。(见 4.3 和 4.4)
If you can't use a synchronous implementation, then you might want to consider approaching the problem like this instead:
如果您不能使用同步实现,那么您可能需要考虑像这样解决问题:
$.Model.extend('Article',
{
findAll : function(params, success, error){
var result = []
db.transaction(function(tx) {
tx.executeSql('select * from contents', [],function(tx, rs) {
for(var i=0; i<rs.rows.length; i++) {
var row = rs.rows.item(i)
result[i] = {
id: row['id'],
title: row['title'],
body: row['body']
}
}
success(result); //toss the result into the 'success' callback
})
})
//here result is undefined
alert(result)
return result
}
})
Article.findAll([], function(view) {
//...
}, function() {
//error occured
});
回答by grosser
I have the same issues, but you might want to use this little wrapper library that makes life easier ;)
我有同样的问题,但你可能想使用这个让生活更轻松的小包装库;)
回答by AutoSponge
I had the same problem, especially on mobile development projects. I created a library that eliminates the need for callbacks: http://code.google.com/p/proto-q/
我遇到了同样的问题,尤其是在移动开发项目上。我创建了一个不需要回调的库:http: //code.google.com/p/proto-q/
This made my code easier to troubleshoot, maintain, and enhance.
这使我的代码更容易进行故障排除、维护和增强。
I added support for AJAX, web workers, script injection, and the storage APIs. I hope it helps.
我添加了对 AJAX、网络工作者、脚本注入和存储 API 的支持。我希望它有帮助。
回答by Austin France
Your trying to use the result synchronously, that is your accessing result before its defined (actually in your code example its not undefined, its an empty array) though I expect thats because you had moved the declaration from its original position trying to figure out what was happening.
您尝试同步使用结果,这是您在定义之前的访问结果(实际上在您的代码示例中它不是未定义的,它是一个空数组),尽管我希望那是因为您已将声明从其原始位置移开,试图找出什么正在发生。
Try this modified example:-
试试这个修改过的例子:-
$.Model.extend('Article',
{
findAll : function(params, success, error){
db.transaction(function(tx) {
tx.executeSql('select * from contents', [], function(tx, rs) {
var result = [];
for(var i=0; i<rs.rows.length; i++) {
var row = rs.rows.item(i)
result.push({
id: row['id'],
title: row['title'],
body: row['body']
});
}
success(result);
});
});
}
});
Article.findAll({}, function(result) {
// process result here
});
Article.findAll() is now an asynchronous function, and its callback (closure) receives the results.
Article.findAll() 现在是一个异步函数,它的回调(闭包)接收结果。

