I'm not sure about whether it's a nice UI (hard to navigate and make selections using the keyboard). That being said, this isn't hard to achieve. Since the read
value will never change, we need to ensure that subscribers are notified (and thus update the bound DOM elements) when the write
function is called, even though the read
value hasn't changed since last time.
The old answer contained a solution using two observables and a manual subscription (which can be seen at http://jsfiddle.net/Emca9/). This edited answer (updated fiddle at http://jsfiddle.net/Emca9/1/) uses two observables, one of which is a writeable computed observable, since that was requested in the comments.
Bind the select
element like the following:
<select data-bind="options: $parent.drivers,
optionsText: 'name',
value: selectedDriver,
optionsCaption: 'Select driver'">
</select>
In the write
function of the computed observable, ensure that subscribers are notified, to ensure they the bindingHandlers update the DOM elements (i.e. the selected option).
function CarViewModel(data){
var self = this;
// ... removed other properties for brevity
self.todaysDriver = ko.observable();
self.selectedDriver = ko.computed({
read: function(){
return undefined;
},
write: function(newValue){
if (newValue){
self.todaysDriver(newValue);
self.selectedDriver.notifySubscribers();
}
}
});
}