Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6c0b011

Browse files
committedJan 11, 2015
refact(ngOptions): specialize readValue and writeValue based on multiple attribute
1 parent 146207e commit 6c0b011

File tree

1 file changed

+43
-40
lines changed

1 file changed

+43
-40
lines changed
 

‎src/ng/directive/ngOptions.js

+43-40
Original file line numberDiff line numberDiff line change
@@ -367,67 +367,70 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
367367

368368

369369
selectCtrl.writeValue = function writeNgOptionsValue(value) {
370-
if (multiple) {
370+
var option = options.getOptionFromViewValue(value);
371371

372-
options.items.forEach(function(option) {
373-
option.element.selected = false;
374-
});
372+
if (option) {
373+
if (selectElement[0].value !== option.selectValue) {
374+
removeUnknownOption();
375+
removeEmptyOption();
375376

376-
if (value) {
377-
value.forEach(function(item) {
378-
var option = options.getOptionFromViewValue(item);
379-
if (option) option.element.selected = true;
380-
});
377+
selectElement[0].value = option.selectValue;
378+
option.element.selected = true;
379+
option.element.setAttribute('selected', 'selected');
381380
}
382-
383381
} else {
384-
var option = options.getOptionFromViewValue(value);
385-
386-
if (option) {
387-
if (selectElement[0].value !== option.selectValue) {
388-
removeUnknownOption();
389-
removeEmptyOption();
390-
391-
selectElement[0].value = option.selectValue;
392-
option.element.selected = true;
393-
option.element.setAttribute('selected', 'selected');
394-
}
382+
if (value === null || providedEmptyOption) {
383+
removeUnknownOption();
384+
renderEmptyOption();
395385
} else {
396-
if (value === null || providedEmptyOption) {
397-
removeUnknownOption();
398-
renderEmptyOption();
399-
} else {
400-
removeEmptyOption();
401-
renderUnknownOption();
402-
}
386+
removeEmptyOption();
387+
renderUnknownOption();
403388
}
404389
}
405390
};
406391

407-
408392
selectCtrl.readValue = function readNgOptionsValue() {
409393

410-
if (multiple) {
411-
412-
return selectElement.val().map(function(selectedKey) {
413-
var option = options.selectValueMap[selectedKey];
414-
return option.viewValue;
415-
});
416-
417-
} else {
394+
var selectedOption = options.selectValueMap[selectElement.val()];
418395

419-
var option = options.selectValueMap[selectElement.val()];
396+
if (selectedOption) {
420397
removeEmptyOption();
421398
removeUnknownOption();
422-
return option ? option.viewValue : null;
399+
return selectedOption.viewValue;
423400
}
401+
return null;
424402
};
425403

426404

405+
// Update the controller methods for multiple selectable options
427406
if (multiple) {
407+
428408
ngModelCtrl.$isEmpty = function(value) {
429409
return !value || value.length === 0;
430410
};
411+
412+
413+
selectCtrl.writeValue = function writeNgOptionsMultiple(value) {
414+
options.items.forEach(function(option) {
415+
option.element.selected = false;
416+
});
417+
418+
if (value) {
419+
value.forEach(function(item) {
420+
var option = options.getOptionFromViewValue(item);
421+
if (option) option.element.selected = true;
422+
});
423+
}
424+
};
425+
426+
427+
selectCtrl.readValue = function readNgOptionsMultiple() {
428+
var selectedValues = selectElement.val() || [];
429+
return selectedValues.map(function(selectedKey) {
430+
var option = options.selectValueMap[selectedKey];
431+
return option.viewValue;
432+
});
433+
};
431434
}
432435

433436

@@ -590,7 +593,7 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
590593
ngModelCtrl.$render();
591594

592595
// Check to see if the value has changed due to the update to the options
593-
if(!ngModelCtrl.$isEmpty(previousValue)) {
596+
if (!ngModelCtrl.$isEmpty(previousValue)) {
594597
var nextValue = selectCtrl.readValue();
595598
if (!equals(previousValue, nextValue)) {
596599
ngModelCtrl.$setViewValue(nextValue);

0 commit comments

Comments
 (0)
This repository has been archived.