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

Commit 5eff109

Browse files
committed
refactor(select, ngOptions): extract common methods; make consistent
1 parent 0c0a1de commit 5eff109

File tree

2 files changed

+15
-21
lines changed

2 files changed

+15
-21
lines changed

src/ng/directive/ngOptions.js

+3-12
Original file line numberDiff line numberDiff line change
@@ -449,12 +449,12 @@ var ngOptionsDirective = ['$compile', '$document', '$parse', function($compile,
449449
if (!multiple) {
450450

451451
selectCtrl.writeValue = function writeNgOptionsValue(value) {
452-
var selectedOption = options.selectValueMap[selectElement.val()];
452+
var selectedOption = selectElement[0].options[selectElement[0].selectedIndex];
453453
var option = options.getOptionFromViewValue(value);
454454

455455
// Make sure to remove the selected attribute from the previously selected option
456456
// Otherwise, screen readers might get confused
457-
if (selectedOption) selectedOption.element.removeAttribute('selected');
457+
if (selectedOption) selectedOption.removeAttribute('selected');
458458

459459
if (option) {
460460
// Don't update the option when it is already selected.
@@ -464,23 +464,14 @@ var ngOptionsDirective = ['$compile', '$document', '$parse', function($compile,
464464

465465
if (selectElement[0].value !== option.selectValue) {
466466
selectCtrl.removeUnknownOption();
467-
selectCtrl.unselectEmptyOption();
468467

469468
selectElement[0].value = option.selectValue;
470469
option.element.selected = true;
471470
}
472471

473472
option.element.setAttribute('selected', 'selected');
474473
} else {
475-
476-
if (value == null && providedEmptyOption) {
477-
selectCtrl.removeUnknownOption();
478-
selectCtrl.selectEmptyOption();
479-
} else if (selectCtrl.unknownOption.parent().length) {
480-
selectCtrl.updateUnknownOption(value);
481-
} else {
482-
selectCtrl.renderUnknownOption(value);
483-
}
474+
selectCtrl.selectUnknownOrEmptyOption(value);
484475
}
485476
};
486477

src/ng/directive/select.js

+12-9
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ var SelectController =
8686

8787
self.unselectEmptyOption = function() {
8888
if (self.hasEmptyOption) {
89-
self.emptyOption.removeAttr('selected');
89+
setOptionSelectedStatus(self.emptyOption, false);
9090
}
9191
};
9292

@@ -128,14 +128,7 @@ var SelectController =
128128
var selectedOption = $element[0].options[$element[0].selectedIndex];
129129
setOptionSelectedStatus(jqLite(selectedOption), true);
130130
} else {
131-
if (value == null && self.emptyOption) {
132-
self.removeUnknownOption();
133-
self.selectEmptyOption();
134-
} else if (self.unknownOption.parent().length) {
135-
self.updateUnknownOption(value);
136-
} else {
137-
self.renderUnknownOption(value);
138-
}
131+
self.selectUnknownOrEmptyOption(value);
139132
}
140133
};
141134

@@ -178,6 +171,16 @@ var SelectController =
178171
return !!optionsMap.get(value);
179172
};
180173

174+
self.selectUnknownOrEmptyOption = function(value) {
175+
if (value == null && self.emptyOption) {
176+
self.removeUnknownOption();
177+
self.selectEmptyOption();
178+
} else if (self.unknownOption.parent().length) {
179+
self.updateUnknownOption(value);
180+
} else {
181+
self.renderUnknownOption(value);
182+
}
183+
};
181184

182185
var renderScheduled = false;
183186
function scheduleRender() {

0 commit comments

Comments
 (0)