Skip to content

Commit

Permalink
Fixes #2151 Problem when valueAllowUnset is true and option value is …
Browse files Browse the repository at this point in the history
…0 and model value is null
  • Loading branch information
mbest committed Dec 3, 2016
1 parent cdd2741 commit 2f230dc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
13 changes: 13 additions & 0 deletions spec/defaultBindings/valueBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,19 @@ describe('Binding: Value', function() {
expect(testNode.childNodes[0].selectedIndex).toEqual(-1);
expect(selected()).toEqual("B");
});

it('Should select no options if model value is null and option value is 0', function() {
var observable = ko.observable(null);
var options = [
{ name: 'B', id: 1 },
{ name: 'A', id: 0 }
];
testNode.innerHTML = "<select data-bind='options:options, optionsValue:\"id\", optionsText:\"name\", value:myObservable, valueAllowUnset:true'></select>";
ko.applyBindings({ myObservable: observable, options: options }, testNode);

expect(testNode.childNodes[0].selectedIndex).toEqual(-1);
expect(observable()).toEqual(undefined);
});
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/binding/selectExtensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
for (var i = 0, n = element.options.length, optionValue; i < n; ++i) {
optionValue = ko.selectExtensions.readValue(element.options[i]);
// Include special check to handle selecting a caption with a blank string value
if (optionValue == value || (optionValue == "" && value === undefined)) {
if (optionValue == value || (optionValue === "" && value === undefined)) {
selection = i;
break;
}
Expand Down

0 comments on commit 2f230dc

Please sign in to comment.