Looking at knockout for the first time, would like to know if I'm approaching it correctly.
Here's a live example: http://jsfiddle.net/fergal_doyle/xhAe8/1/
I have a "car" select, and an "other" input to allow values not present in the select box. When "other" is populated, it over-rides "car" by disabling it and a computed observable is set to the actual chosen car. Only when "other" is emptied, will "car" become enabled again.
HTML:
<label for="car">Car</label>
<select id="car" data-bind="options: cars, value: selectedCar, enable: checkCar"></select>
<label for="other">Other</label>
<input id="other" type="text" data-bind="value: other, valueUpdate:'afterkeydown'"/>
<p>Chosen value: <span data-bind="text: chosenCar"></span></p>
JS:
function viewModel(){
var self = this;
self.cars = ko.observableArray(["Chrysler", "Chevrolet", "Ford", "Lincoln"]);
self.other = ko.observable("");
self.selectedCar = ko.observable("");
self.chosenCar = ko.computed(function() {
return self.other() || self.selectedCar();
});
self.checkCar = ko.computed(function(){
if(self.other())
return false;
else
return true;
});
}
ko.applyBindings(new viewModel());
if(self.other()) return false; else return true;
is a bit verbose, I'd just writereturn !self.other();
\$\endgroup\$