Skip to content
This repository has been archived by the owner on May 21, 2021. It is now read-only.

Commit

Permalink
fix(options): mark options as selected on change of options even if o…
Browse files Browse the repository at this point in the history
…ptions are objects

Internal Option representation is an object which is created newly on each change of options collection. Array#indexOf with objects compares by reference and not equality. Because of that the option put into ngModel out site are not marked as selected.
By calculating and comparing hash codes of the option the directive is now independent of the kind of options.

Closes #7
  • Loading branch information
Philipp Burgmer committed Jun 10, 2014
1 parent 02c2b97 commit 609c0b4
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/w11k-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,20 +458,25 @@ angular.module('w11k.select').directive('w11kSelect', [
var optionsExpParsed = optionParser.parse(optionsExp);

function collection2options(collection, viewValue) {
var viewValueHashes = viewValue.map(function (selectedValue) {
return hashCode(selectedValue);
});

return collection.map(function (option) {
var optionValue = modelElement2value(option);
var optionValueHash = hashCode(optionValue);
var optionLabel = modelElement2label(option);

var selected;
if (angular.isArray(viewValue) && viewValue.indexOf(optionValue) !== -1) {
if (viewValueHashes.indexOf(optionValueHash) !== -1) {
selected = true;
}
else {
selected = false;
}

return {
hash: hashCode(option).toString(36),
hash: optionValueHash.toString(36),
label: optionLabel,
model: option,
selected: selected
Expand Down

0 comments on commit 609c0b4

Please sign in to comment.