Skip to content

Commit

Permalink
Merge pull request #2192 from knockout/2192-value-select-undefined
Browse files Browse the repository at this point in the history
value binding when valueAllowUnset is true
  • Loading branch information
mbest committed Apr 9, 2017
2 parents 4391eb9 + 0a1581e commit 941a1f2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
11 changes: 11 additions & 0 deletions spec/defaultBindings/valueBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,17 @@ describe('Binding: Value', function() {
expect(select.selectedIndex).toEqual(0);
});

it('Should display the caption when the model value changes to undefined after having no selection', function() {
var observable = ko.observable('B');
testNode.innerHTML = "<select data-bind='options:[\"A\", \"B\"], optionsCaption:\"Select...\", value:myObservable, valueAllowUnset:true'></select>";
ko.applyBindings({ myObservable: observable }, testNode);
var select = testNode.childNodes[0];

select.selectedIndex = -1;
observable(undefined);
expect(select.selectedIndex).toEqual(0);
});

it('Should select no option value if no option value matches the current model property value', function() {
var observable = ko.observable();
testNode.innerHTML = "<select data-bind='options:[\"A\", \"B\"], value:myObservable, valueAllowUnset:true'></select>";
Expand Down
4 changes: 2 additions & 2 deletions src/binding/defaultBindings/value.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ ko.bindingHandlers['value'] = {
return;
}

var valueHasChanged = (newValue !== elementValue);
var valueHasChanged = newValue !== elementValue;

if (valueHasChanged) {
if (valueHasChanged || elementValue === undefined) {
if (tagName === "select") {
var allowUnset = allBindings.get('valueAllowUnset');
ko.selectExtensions.writeValue(element, newValue, allowUnset);
Expand Down

0 comments on commit 941a1f2

Please sign in to comment.