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

perf(ngOptions): prevent initial options repainting #16071

Merged
merged 2 commits into from
Jun 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/ng/directive/ngOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,9 @@ var ngOptionsDirective = ['$compile', '$document', '$parse', function($compile,
if (!multiple) {

selectCtrl.writeValue = function writeNgOptionsValue(value) {
// The options might not be defined yet when ngModel tries to render
if (!options) return;

var selectedOption = selectElement[0].options[selectElement[0].selectedIndex];
var option = options.getOptionFromViewValue(value);

Expand Down Expand Up @@ -504,9 +507,11 @@ var ngOptionsDirective = ['$compile', '$document', '$parse', function($compile,
} else {

selectCtrl.writeValue = function writeNgOptionsMultiple(values) {
// The options might not be defined yet when ngModel tries to render
if (!options) return;

// Only set `<option>.selected` if necessary, in order to prevent some browsers from
// scrolling to `<option>` elements that are outside the `<select>` element's viewport.

var selectedOptions = values && values.map(getAndUpdateSelectedOption) || [];

options.items.forEach(function(option) {
Expand Down Expand Up @@ -588,10 +593,6 @@ var ngOptionsDirective = ['$compile', '$document', '$parse', function($compile,

}

// We need to do this here to ensure that the options object is defined
// when we first hit it in writeNgOptionsValue
updateOptions();

// We will re-render the option elements if the option values or labels change
scope.$watchCollection(ngOptions.getWatchables, updateOptions);

Expand Down
62 changes: 60 additions & 2 deletions test/ng/directive/ngOptionsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe('ngOptions', function() {

var scope, formElement, element, $compile, linkLog, ngModelCtrl;
var scope, formElement, element, $compile, linkLog, childListMutationObserver, ngModelCtrl;

function compile(html) {
formElement = jqLite('<form name="form">' + html + '</form>');
Expand Down Expand Up @@ -151,7 +151,18 @@ describe('ngOptions', function() {
$compile(element.contents())(scope);
}
};
});
})

.directive('observeChildList', function() {
return {
link: function(scope, element) {
var config = { childList: true };

childListMutationObserver = new window.MutationObserver(noop);
childListMutationObserver.observe(element[0], config);
}
};
});

$provide.decorator('ngOptionsDirective', function($delegate) {

Expand Down Expand Up @@ -801,6 +812,29 @@ describe('ngOptions', function() {
expect(options[2]).toBeMarkedAsSelected();
});


if (window.MutationObserver) {
//IE9 and IE10 do not support MutationObserver
//Since the feature is only needed for a test, it's okay to skip these browsers
it('should render the initial options only one time', function() {
scope.value = 'black';
scope.values = ['black', 'white', 'red'];
// observe-child-list adds a MutationObserver that we will read out after ngOptions
// has been compiled
createSelect({
'ng-model':'value',
'ng-options':'value.name for value in values',
'observe-child-list': ''
});

var optionEls = element[0].querySelectorAll('option');
var records = childListMutationObserver.takeRecords();

expect(records.length).toBe(1);
expect(records[0].addedNodes).toEqual(optionEls);
});
}

describe('disableWhen expression', function() {

describe('on single select', function() {
Expand Down Expand Up @@ -2966,6 +3000,30 @@ describe('ngOptions', function() {
optionsSetSelected[0].calls.reset();
optionsSetSelected[1].calls.reset();
});

if (window.MutationObserver) {
//IE9 and IE10 do not support MutationObserver
//Since the feature is only needed for a test, it's okay to skip these browsers
it('should render the initial options only one time', function() {
scope.value = ['black'];
scope.values = ['black', 'white', 'red'];
// observe-child-list adds a MutationObserver that we will read out after ngOptions
// has been compiled
createSelect({
'ng-model':'selected',
'ng-options':'value.name for value in values',
'multiple': 'true',
'observe-child-list': ''
});

var optionEls = element[0].querySelectorAll('option');
var records = childListMutationObserver.takeRecords();

expect(records.length).toBe(1);
expect(records[0].addedNodes).toEqual(optionEls);
});
}

});


Expand Down