Knockout observed effect

How to add all values ​​to observableArray in one go? Adding values ​​to the loop is very slow in my case. Here is a jsfiddle example. jsfiddle

+7
source share
3 answers

As you clear the entire observable array, one of the ways you can do this is:

 var viewModel = { name: "base", addingValue:new ko.observable(), someArr: new ko.observableArray(["123","432","sdafasd","xrere"]), add: function() { this.someArr.push(this.addingValue()); }, updateSomeArr:function() { var temp = []; for(var i=0;i<5;i++) { temp.push("555565"); } this.someArr(temp); } } 
+7
source
 var myArray = ko.observableArray([]); var valuesToInsert = [1,2,3]; myArray.push.apply(myArray, valuesToInsert); 

what he

+17
source

There is already a selected answer, but I thought the following would help. You can disable the observation behavior by executing your observable array to get the implementation of the underlying array:

 var underlyingArray = viewModel.someArr(); 

Then you can add elements to underlyingArray without disabling someArr events. When you are finished adding items, call:

 viewModel.someArr.valueHasMutated(); 

This will cause the event to trigger a notification of all observable viewModel.someArr () dependent.

+3
source

All Articles