Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

knockout observableArray performance

How to add all values to observableArray in one time? Adding values in loop works very slow in my case. Here is jsfiddle example. jsfiddle

like image 986
Neir0 Avatar asked Nov 02 '11 13:11

Neir0


People also ask

What is Ko observableArray?

If you want to detect and respond to changes of a collection of things, use an observableArray . This is useful in many scenarios where you're displaying or editing multiple values and need repeated sections of UI to appear and disappear as items are added and removed.

How do I sort knockout observable array?

Description. The KnockoutJS Observable sort() method sorts all items in the array. By default, items are sorted in an ascending order. For sorting an array in a descending order, use reverse() method on sorted array.

How do you update items in observableArray knockout?

You should look at defining the object structure for each element of your array and then add elements of that type to your observable array. Once this is done, you will be able to do something like item. productQuantity(20) and the UI will update itself immediately. EDIT Added the function provided by the OP :).


2 Answers

var myArray = ko.observableArray([]);
var valuesToInsert = [1,2,3];
myArray.push.apply(myArray, valuesToInsert);

that's it

like image 134
AGS Avatar answered Oct 07 '22 07:10

AGS


Since you're clearing out the entire observable array, one way you can accomplish 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);
    }
}
like image 21
Jim D'Angelo Avatar answered Oct 07 '22 07:10

Jim D'Angelo