knockout/knockout — pull request #1623 by mbest (2014-11-18 23:43:42+00:00)
Title: valueAllowUnset - null selection not maintained when option items update

When using `valueAllowUnset: true` with the `options`, `value`, and `optionsText` bindings on a `` element, it is possible to get the view model's value out of sync with the `` element in the user interface.

**Steps to reproduce**

1. Initially have no selection in the `` element (underlying observable model property has value of `null`)
2. Make an update to an observable property of one of the items in the array that the `` is bound to
3. The `` then updates to automatically select the first item in the options array
4. But the underlying observable model property remains `null`

Note that if the `` already contains a selection then everything works as expected. In addition, adding/removing items seems to work correctly too.

It is only when the `` doesn't have an initial selection and a modification is made to an existing item in the bound array that this behaviour is observed.

I believe the correct behaviour should be to leave the `` element as-is, without updating its selected value. That is, the model property should always be respected when `valueAllowUnset: true` is applied.

**Example** (fiddle at http://jsfiddle.net/spoida/f8b7LL2b/)

In the following example, if you leave the selection blank and click the "Update name" button, the `` element visually shows the first item in the list as selected but in fact the view model property is still `null`, which can be seen by clicking on "Alert selection".

**HTML**
```html

Update name
Clear selection
Alert selection
```

**Javascript**
```javascript
var demo = { };

demo.person = function(id, name) {
var self = this;
self.PersonID = id;
self.Name = ko.observable(name);
};

demo.viewModel = function() {
var self = this;

self.people = ko.observableArray([]);
self.people.push(new demo.person(1, 'Adam'));
self.people.push(new demo.person(2, 'Belinda'));
self.people.push(new demo.person(3, 'Chris'));
self.people.push(new demo.person(4, 'Dianna'));

self.selectedPerson = ko.observable(null);

self.updatePersonName = function () {
self.people()[2].Name(self.people()[2].Name() + ' abc');
};

self.clearSelection = function () {
self.selectedPerson(null);
};

self.alertCurrentSelection = function () {
var message = 'No person is selected';
if (self.selectedPerson() !== null) {
message = self.selectedPerson().Name() + ' is selected';
}
alert(message);
};
};

ko.applyBindings(new demo.viewModel());
```