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

fix(select): don't call $render twice if $viewValue ref changes #11412

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 3 additions & 1 deletion src/ng/directive/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,13 @@ var selectDirective = function() {

// we have to do it on each watch since ngModel watches reference, but
// we need to work of an array, so we need to see if anything was inserted/removed
var lastView, lastViewRef = NaN;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lastView is already declared up top isn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True - now that I see it, isn't it a bit weird that it is defined outside the link fn? So every selectDirective uses the same variable?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree so I moved it inside.

scope.$watch(function selectMultipleWatch() {
if (!equals(lastView, ngModelCtrl.$viewValue)) {
if (lastViewRef === ngModelCtrl.$viewValue && !equals(lastView, ngModelCtrl.$viewValue)) {
lastView = shallowCopy(ngModelCtrl.$viewValue);
ngModelCtrl.$render();
}
lastViewRef = ngModelCtrl.$viewValue;
});

// If we are a multiple select then value is now a collection
Expand Down
54 changes: 54 additions & 0 deletions test/ng/directive/selectSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,60 @@ describe('select', function() {
expect(element).toBeDirty();
});


describe('calls to $render', function() {

var ngModelCtrl;

beforeEach(function() {
compile(
'<select name="select" ng-model="selection" multiple>' +
'<option>A</option>' +
'<option>B</option>' +
'</select>');

ngModelCtrl = element.controller('ngModel');
spyOn(ngModelCtrl, '$render').andCallThrough();
});


it('should call $render once when the reference to the viewValue changes', function() {
scope.$apply(function() {
scope.selection = ['A'];
});
expect(ngModelCtrl.$render.calls.length).toBe(1);

scope.$apply(function() {
scope.selection = ['A', 'B'];
});
expect(ngModelCtrl.$render.calls.length).toBe(2);

scope.$apply(function() {
scope.selection = [];
});
expect(ngModelCtrl.$render.calls.length).toBe(3);
});


it('should call $render once when the viewValue deep-changes', function() {
scope.$apply(function() {
scope.selection = ['A'];
});
expect(ngModelCtrl.$render.calls.length).toBe(1);

scope.$apply(function() {
scope.selection.push('B');
});
expect(ngModelCtrl.$render.calls.length).toBe(2);

scope.$apply(function() {
scope.selection.length = 0;
});
expect(ngModelCtrl.$render.calls.length).toBe(3);
});

});

});


Expand Down