Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suspend bindings in knockout.js 1.2.1

Is there any option to suspend and resume bindings in knockout?

Version : knockout.js 1.2.1

Our need for suspending bindings stems from the following. During some operations we have to load a lot of data from the server, eg multiple selects have their entire data changed, there are tables whose rows are dynamically added etc.

Now in this current scenario, the form is fully bound with the view model. When we clear the combos and add each item, the view gets refreshed hence there is significant delay. If I had the means to suspend binding, I could suspend, then load all data into the viewmodel and then resume binding again.

like image 522
Pramod Pallath Vasudevan Avatar asked Feb 21 '12 17:02

Pramod Pallath Vasudevan


2 Answers

I don't think there is a way to suspend binding in knockout.js. Without seeing the code it's hard to say, but the slowness is probably caused by the fact that you refresh you observableArrays by clearing them and adding new items one by one. Instead you can refresh the entire array at once:

...
self.manyItems = ko.observableArray();
...
function refreshItems(newItems){
    self.manyItems(newItems);
}
like image 139
Roman Bataev Avatar answered Oct 23 '22 18:10

Roman Bataev


Suspending and resuming is possible: take a look at this demo put together by Ryan Niemeyer. Refer to this entry on his blog for more background information.

like image 42
mhu Avatar answered Oct 23 '22 20:10

mhu