How to trigger several changes as "unit" with knockout.js
Asked Answered
B

2

5

I have a small app filtering a list of items by providing several choices for different attributes in combobox (select) elements. Everything works fine selecting and deselecting single combobox elements, but since I introduced a required button to "Reset Filter settings" which resets all combobox elements to null I get poor performance. It seems that each single statement modifying a comboxbox is triggering a refresh of the complete list.

How can I tell knockout to STOP updating the observables, have all combobox elements reset to null and then tell knockout to RESUME updating or initially TRIGGER the update myself.

Any ideas?

Thanks Andreas

Bondie answered 28/4, 2012 at 22:12 Comment(1)
Take a look at the "throttle" extender: knockoutjs.com/documentation/throttle-extender.htmlInfantile
O
5

You could pause the computed property(or properties) managing the list while you reset all your combos.

See http://www.knockmeout.net/2011/04/pausing-notifications-in-knockoutjs.html

Ombudsman answered 28/4, 2012 at 22:39 Comment(1)
That is exactly the thing I was searching. Thanks a lot!Bondie
M
4

Shamelessly expanding on Niko's comment, you should use the throttle extender.

Adding .extend({ throttle: 10 }) to my computed declaration fixed this problem for me:

// get only selected markets
self.SelectedMarkets = ko.computed(function() {
    return ko.utils.arrayFilter(self.Markets(), function(market) {
        return market.IsSelected() == 1; });
}).extend({ throttle: 10 });
Morganite answered 29/4, 2012 at 5:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.