BigPoppa
BigPoppa

Reputation: 1215

KnockoutJS Options Bindings

So I have say 5 select lists on a single page. I have options binding on each one of them, that are all bound to the same data set, for example a state model.

Once someone changes the state on one of the selects, I would like to take action on it.

Any ideas?

Thanks!

Upvotes: 0

Views: 684

Answers (2)

s0nica
s0nica

Reputation: 1307

If I understood your question correctly, I imagine you have one ko.observable variable per select list that keeps track of the currently selected value. If you want to take action everytime one of these index changes, then you can use the subscribe function:

var viewModel = {
   firstSelectListIndex : ko.observable(), // bound to the first select list value
   secondSelectListIndex : ko.observable(),
   ...   
};

then if you are interested in taking action when the user changes the state of the first select list, you can do:

viewModel.firstSelectListIndex.subscribe(function(newValue) { 
    // your code
});

and you have the new value, associated to the new selected item in the list, passed as a parameter.

Upvotes: 0

Jason More
Jason More

Reputation: 7073

Generally you use a ko.computed to do handle things when values change: http://knockoutjs.com/documentation/computedObservables.html

But if you want to watch when a specific observable changes, you use subscribe : http://knockoutjs.com/documentation/observables.html

You can fork and update this jsfiddle to send me a code example if you run into an issue: http://jsfiddle.net/JasonMore/p6Vcc/

Upvotes: 1

Related Questions