jeudi 25 décembre 2014

KnockoutJS: Infinite recursion while modifying observable array within the subscription callback


I can't figure out why the following code snippet leads to infinite recursion:



var observableArray = ko.observableArray();
observableArray.subscribe(function (changes) {
changes.forEach(function(change) {
console.log("change: " + JSON.stringify(change));
if (change.status === 'added') {
if (change.value === "first") {
observableArray.push("second");
}
}
});
}, null, "arrayChange");

observableArray.push("first");


And here is the console log:



change: {"status":"added","value":"first","index":0}
change: {"status":"added","value":"first","index":0}
change: {"status":"added","value":"first","index":0}
change: {"status":"added","value":"first","index":0}
change: {"status":"added","value":"first","index":0}
...
Uncaught RangeError: Maximum call stack size exceeded


I really don't get what is going on here. Could anyone help me, please?





Aucun commentaire:

Enregistrer un commentaire