Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit a3680e1

Browse files
committed
simplify
1 parent d73ab07 commit a3680e1

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

src/ng/directive/ngOptions.js

+18-19
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,12 @@ var ngOptionsDirective = ['$compile', '$document', '$parse', function($compile,
592592

593593
// ------------------------------------------------------------------ //
594594

595+
function addOptionElement(option, parent) {
596+
var optionElement = optionTemplate.cloneNode(false);
597+
parent.appendChild(optionElement);
598+
updateOptionElement(option, optionElement);
599+
}
600+
595601

596602
function updateOptionElement(option, element) {
597603
option.element = element;
@@ -612,11 +618,12 @@ var ngOptionsDirective = ['$compile', '$document', '$parse', function($compile,
612618
var previousValue = options && selectCtrl.readValue();
613619

614620
// We must remove all current options, but cannot simply set innerHTML = null
615-
// since the providedOption might have an ngIf on it that inserts comments which we must
616-
// preserve
617-
// Instead iterate over the current option elements and remove them or their optgroup
621+
// since the providedEmptyOption might have an ngIf on it that inserts comments which we
622+
// must preserve.
623+
// Instead, iterate over the current option elements and remove them or their optgroup
618624
// parents
619625
if (options) {
626+
620627
for (var i = options.items.length - 1; i >= 0; i--) {
621628
var option = options.items[i];
622629
if (option.group) {
@@ -629,26 +636,24 @@ var ngOptionsDirective = ['$compile', '$document', '$parse', function($compile,
629636

630637
options = ngOptions.getOptions();
631638

632-
var groupMap = {};
639+
var groupElementMap = {};
633640
var listFragment = $document[0].createDocumentFragment();
634641

635642
// Ensure that the empty option is always there if it was explicitly provided
636643
if (providedEmptyOption) {
637644
selectElement.prepend(emptyOption);
638645
}
639646

640-
options.items.forEach(function updateOption(option) {
641-
var group;
647+
options.items.forEach(function addOption(option) {
642648
var groupElement;
643-
var optionElement;
644649

645650
if (isDefined(option.group)) {
646651

647652
// This option is to live in a group
648653
// See if we have already created this group
649-
group = groupMap[option.group];
654+
groupElement = groupElementMap[option.group];
650655

651-
if (!group) {
656+
if (!groupElement) {
652657

653658
groupElement = optGroupTemplate.cloneNode(false);
654659
listFragment.appendChild(groupElement);
@@ -657,21 +662,15 @@ var ngOptionsDirective = ['$compile', '$document', '$parse', function($compile,
657662
groupElement.label = option.group;
658663

659664
// Store it for use later
660-
group = groupMap[option.group] = {
661-
groupElement: groupElement
662-
};
663-
665+
groupElementMap[option.group] = groupElement;
664666
}
665667

666-
optionElement = optionTemplate.cloneNode(false);
667-
group.groupElement.appendChild(optionElement);
668-
updateOptionElement(option, optionElement);
668+
addOptionElement(option, groupElement);
669669

670670
} else {
671671

672-
optionElement = optionTemplate.cloneNode(false);
673-
listFragment.appendChild(optionElement);
674-
updateOptionElement(option, optionElement);
672+
// This option is not in a group
673+
addOptionElement(option, listFragment);
675674
}
676675
});
677676

0 commit comments

Comments
 (0)