1

HTML5indexedDBのAPIとしてJaydataを使用しています。indexedDBにテーブルがあり、再帰的にクエリを実行する必要があります。プロセス全体が完了したら、コールバックが必要です。以下は再帰関数です。すべてが完了したら、コールバックが必要です。

function getData(idValue) {
    myDB.MySplDB
        .filter( function(val) {
            return val.ParentId == this.parentId;
        }, {parentId: idvalue})
        .toArray( function(vals) {
            if(vals.length < 1) {
                // some operation to store the value
            } else {
                for (var j=0;j<vals.length;j++) {
                    getData(vals[j].Id);
                }
            }
        });
}

完了する前に呼び出されるため、への追加は機能.done(function(){...});.toArrayません。

4

2 に答える 2

1

(免責事項: 私は JayData で働いています)

プロセス全体の終了を待つには、promise を使用する必要があります。約束は必ず返さなければなりません。ループの中でややこしくなりますが、super promise を返します。したがって、コードは次のようになります。

function getData(idValue) {
    return myDB.MySplDB
    .filter( function(val) {
        return val.ParentId == this.parentId;
    }, {parentId: idvalue})
    .toArray( function(vals) {
        if(vals.length < 1) {
            // some operation to store the value
            // important: return a promise from here, like:
            return myDB.saveChanges(); 
        } else {
            var promises = [];
            for (var j=0;j<vals.length;j++) {
                promises.push(getData(vals[j].Id));
            }
            return $.when.apply(this, promises);
        }
    });
}

getData(1)
.then(function() {
        // this will run after everything is finished
});

備考:

  1. この例では jQuery promise を使用しているため、jQuery 1.8+ $.when が varargs を使用する必要があるため、apply が必要です。

  2. これは、わずかに異なる構文の q promise で機能します

于 2013-03-05T14:52:43.780 に答える
0

この疑似コードはあなたの場合に意味がありますか?

var helper = function (accu) {
 // Take an id from the accumulator
 // query the db for new ids, and in the db query callback : 
   // If there is child, do "some operation to store the value" (I'm not sure what you're trying to do here
   // Else add the child to the accumulator
   // if accu is empty, call the callback, it means you reached the end

getData() は、最初の ID と最終的なコールバックを含むアキュムレータを使用してこのヘルパーを呼び出します

于 2013-03-05T14:01:04.877 に答える