6

私はobservableArrayを持っています。observableArray からアイテムを削除または追加した後、およびすべての依存サブスクリプション呼び出しが完了した後に、関数を実行したいと考えています。お気に入り :

 observableArray.push(newObject);

 //I can't put my function call at this point because if any subscription is..
 //with newObject or observableArray will execute asynch, and i.. 
 //want my function to execute after all such subscription execution.  

ノックアウトでこれを達成する方法はありますか?

4

2 に答える 2

1

observableArray.push()trueが返されるかどうかはわかりませんが、これを試してみてください。

if (observableArray.push(newObject)) {
    console.log(observableArray);
}
于 2013-03-08T14:00:20.633 に答える
1

イベントは非同期で発生すると思ったので、次のLive JSFiddleを書きました 。

var flagUpdater = ko.observable(0),
    aList = ko.observableArray(["Foo", "Bar", "Baz"]);

flagUpdater.subscribe(function() {
  console.log("Change the flag now!");
});

aList.subscribe(function() {
  console.log("Schedule flag update");
  flagUpdater("blah");
});

aList.push("Qoo");

しかし、うまくいきません。コールバックはすべて同期的に処理されているようです。つまり、修飾子関数 (push()たとえば) が返されると、すべてのコールバックが既に返されています。したがって、配列を操作した後にフラグを設定するだけです(ライブ) :

aList.push("Qoo");
flag = "CHANGED";
console.log("Flag is now " + flag);
于 2013-03-08T12:50:13.693 に答える