Given an object
var viewModel = {
Opts: ko.observableArray([
{ d: 'a', v: 0, selected: 1},
{ d: 'b', v: 1, selected: 1},
{ d: 'c', v: 2, selected: 1},
{ d: 'd', v: 3, selected: 2},
{ d: 'e', v: 4, selected: 1},
{ d: 'f', v: 5, selected: 1}
]),
selectedOpts: ko.observableArray([])
};
I want to bind a multiple select list to change the values of the ‘selected’ property, 1 being false, 2 being true. (how it is represented on the server – it is actually status codes)
<select data-bind="options: Opts,
optionsText: 'd',
optionsValue: 'v',
selectedOptions: selectedOpts,
optionsCaption: 'Choose...'" multiple=""></select>
I know that I wouldn’t need the selectedOpts object if I could bind the selectedOptions binding to the selected property, I am just not getting how to do this simply without setting up a manual subscription to selectedOpts to see the list of values and manually setting the selected property on each item that has been selected.
I am sure there is an easy way.
Thanks
Manual subscriptions aren’t so scary and can be very useful. I’ve modified your fiddle to demonstrate how easy this would be to accomplish if you just make the selected property observable:
http://jsfiddle.net/CsrrD/4/
Just a tip from a design perspective, the subscription function should probably live in your viewModel object and you may want to use the mapping plugin to avoid manual observable creation, but you get the idea.