Skip to content

Commit 6f7018d

Browse files
wesleychoIgorMinar
authored andcommitted
perf(select): execute render after $digest cycle
This is an optimization to defer execution of the render function in the select directive after the $digest cycle completes inside the $watchCollection expressions. This does a check to see if the render function is already registered in the $$postDigestQueue before it passes it into $$postDigest, guaranteeing that the DOM manipulation happens only in one execution after the model settles. Closes angular#8825
1 parent 4f9ac07 commit 6f7018d

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/ng/directive/select.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
224224
optionsExp = attr.ngOptions,
225225
nullOption = false, // if false, user will not be able to select it (used by ngOptions)
226226
emptyOption,
227+
renderScheduled = false,
227228
// we can't just jqLite('<option>') since jqLite is not smart enough
228229
// to create it in <select> and IE barfs otherwise.
229230
optionTemplate = jqLite(document.createElement('option')),
@@ -413,9 +414,19 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
413414

414415
ctrl.$render = render;
415416

416-
scope.$watchCollection(valuesFn, render);
417+
scope.$watchCollection(valuesFn, function () {
418+
if (!renderScheduled) {
419+
scope.$$postDigest(render);
420+
renderScheduled = true;
421+
}
422+
});
417423
if ( multiple ) {
418-
scope.$watchCollection(function() { return ctrl.$modelValue; }, render);
424+
scope.$watchCollection(function() { return ctrl.$modelValue; }, function () {
425+
if (!renderScheduled) {
426+
scope.$$postDigest(render);
427+
renderScheduled = true;
428+
}
429+
});
419430
}
420431

421432
function getSelectedSet() {
@@ -438,6 +449,8 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
438449

439450

440451
function render() {
452+
renderScheduled = false;
453+
441454
// Temporary location for the option groups before we render them
442455
var optionGroups = {'':[]},
443456
optionGroupNames = [''],

0 commit comments

Comments
 (0)