I want to bind boolean value to select element using KO v2.1.0, but obviously it doesn\'t work as expected.
html code:
Here is an option that we explored for this one from this forum post:
ko.bindingHandlers.booleanValue = {
init: function(element, valueAccessor, allBindingsAccessor) {
var observable = valueAccessor(),
interceptor = ko.computed({
read: function() {
return observable().toString();
},
write: function(newValue) {
observable(newValue === "true");
}
});
ko.applyBindingsToNode(element, { value: interceptor });
}
};
So, we use a custom binding to inject a writeable computed observable between the UI and our view model. This is just an alternative to doing it directly in your view model.
Sample here: http://jsfiddle.net/rniemeyer/H4gpe/