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

Commit a0dd9b0

Browse files
committed
refactor(select, ngOptions): extract common methods; make consistent
1 parent 498bef1 commit a0dd9b0

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
@@ -450,12 +450,12 @@ var ngOptionsDirective = ['$compile', '$document', '$parse', function($compile,
450450
if (!multiple) {
451451

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

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

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

466466
if (selectElement[0].value !== option.selectValue) {
467467
selectCtrl.removeUnknownOption();
468-
selectCtrl.unselectEmptyOption();
469468

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

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

src/ng/directive/select.js

+12-9
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ var SelectController =
8787

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

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

@@ -179,6 +172,16 @@ var SelectController =
179172
return !!optionsMap.get(value);
180173
};
181174

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

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

0 commit comments

Comments
 (0)