Callback, return value and HTML5 executeSql function

前端 未结 4 1411
轻奢々
轻奢々 2020-12-18 08:19

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\',
{
     fin         


        
4条回答
  •  南方客
    南方客 (楼主)
    2020-12-18 08:49

    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

    Article.findAll() is now an asynchronous function, and its callback (closure) receives the results.

提交回复
热议问题