Knockout select binding bug
HTML
<script src="http://knockoutjs.com/downloads/knockout-3.0.0.debug.js"></script>
<select data-bind="options: opts, value: val, optionsText: 'Text', optionsValue: 'Id', optionsCaption: 'Select an Item'"></select>
<span>Currently selected: <span data-bind="text: val"></span></span>
JavaScript
ko.bindingHandlers['value']['update'] = function (element, valueAccessor) {
var valueIsSelectOption = ko.utils.tagNameLower(element) === "select";
var newValue = ko.utils.unwrapObservable(valueAccessor());
var elementValue = ko.selectExtensions.readValue(element);
var valueHasChanged = (newValue !== elementValue);
if (valueHasChanged) {
var applyValueAction = function () { ko.selectExtensions.writeValue(element, newValue); };
applyValueAction();
if (valueIsSelectOption) {
/*if (newValue !== ko.selectExtensions.readValue(element)) {
// If you try to set a model value that can't be represented in an already-populated dropdown, reject that change,
// because you're not allowed to have a model value that disagrees with a visible UI selection.
ko.dependencyDetection.ignore(ko.utils.triggerEvent, null, [element, "change"]);
} else {*/
// Workaround for IE6 bug: It won't reliably apply values to SELECT nodes during the same execution thread
// right after you've changed the set of OPTION nodes on it. So for that node type, we'll schedule a second thread
// to apply the value as well.
setTimeout(applyValueAction, 0);
//}
}
}
};
ko.bindingHandlers['options']['update'] = function (element, valueAccessor, allBindings) {
function selectedOptions() {
return ko.utils.arrayFilter(element.options, function (node) { return node.selected; });
}
var selectWasPreviouslyEmpty = element.length == 0;
var previousScrollTop = (!selectWasPreviouslyEmpty && element.multiple) ? element.scrollTop : null;
var unwrappedArray = ko.utils.unwrapObservable(valueAccessor());
var includeDestroyed = allBindings.get('optionsIncludeDestroyed');
var captionPlaceholder = {};
var captionValue;
var previousSelectedValues;
if (element.multiple) {
...