SteveSanderson/knockout — pull request #313 by jamesallardice (2012-02-06 18:42:25+00:00)
Title: Maintain selection when adding new options to select element

When adding a new `option` element to a `select` with an `options` binding, the newly added `option` is selected instead if it has a `value` equal to that of any of the previously selected options (see http://jsfiddle.net/YuQsz/2/).

This is because it currently only checks `value` to work out which option was previously selected after rebuilding the list. When multiple options have the same value, the last one will become selected, regardless of whether it was previously selected or not. In a multiple `select`, all options with the same value will become selected.

This fix checks for option value and text content before deciding the option was previously selected. This is still not completely fool proof as it won't work for options that share both a value and text content, but that is unlikely to happen very often.

Change to selectExtensions.js - added method to return text content of option (is this necessary? Is there another method somewhere that can already do this?)

Change to defaultBinding.js - modified section that selects options after appending new set to the DOM, and section that builds array of previously selected option values.