Reputation: 16420
I have a select:
<select class="form-control" data-bind="value: title.value, foreach: title.choices">
<option data-bind="text: name, value: code"></option>
</select>
With Js:
self.title.choices = [
{ "code": "", "name": "" },
{ "code": "MR", "name": "Mr" },
{ "code": "MRS", "name": "Mrs" }
]
self.title.value = ko.observable('').extend({
required: true
});
self.title.value.subscribe(function (newValue) {
self.gender.value('male');
});
I then call applyBindings, and the select list iterates over the options, and fire's my subscribe function 3 times (1x for each option). It annoyingly then updates my other observable, how can I prevent this happening?
Upvotes: 0
Views: 70
Reputation: 134841
Use the options
binding instead.
<select class="form-control" data-bind="value: title.value,
options: title.choices,
optionsText: 'code',
optionsValue: 'name',
optionsCaption: ''">
</select>
self.title.choices = [
{ "code": "MR", "name": "Mr" },
{ "code": "MRS", "name": "Mrs" }
];
self.title.value = ko.observable('').extend({
required: true
});
Upvotes: 3