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

fix(rootScope): $watchCollection returns in oldCollection a copy of the ... #5688

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
6 changes: 5 additions & 1 deletion src/ng/rootScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ function $RootScopeProvider(){
$watchCollection: function(obj, listener) {
var self = this;
var oldValue;
var oldArray;
var newValue;
var changeDetected = 0;
var objGetter = $parse(obj);
Expand All @@ -424,6 +425,7 @@ function $RootScopeProvider(){

function $watchCollectionWatch() {
newValue = objGetter(self);
oldArray = null;
var newLength, key;

if (!isObject(newValue)) {
Expand All @@ -439,6 +441,8 @@ function $RootScopeProvider(){
changeDetected++;
}

oldArray = oldValue.length > 0 ? Array.prototype.slice.call(oldValue, 0) : [];
Copy link
Contributor

Choose a reason for hiding this comment

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

this should be:

if (listener.length > 1) {
  oldArray =  slice(oldValue, 0);
}

or something along those lines. the important bit is listener.length - we don't want to compute this value if the listener is not asking for it. (we'll break the listener if it uses the arguments object to get hold of args, but I think it's a fair trade-off for hot code like this.

Copy link
Contributor

Choose a reason for hiding this comment

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

we also have a slice fn defined in Angular.js file, so you should use it


newLength = newValue.length;

if (oldLength !== newLength) {
Expand Down Expand Up @@ -492,7 +496,7 @@ function $RootScopeProvider(){
}

function $watchCollectionAction() {
listener(newValue, oldValue, self);
listener(newValue, oldArray || oldValue, self);
}

return this.$watch($watchCollectionWatch, $watchCollectionAction);
Expand Down
59 changes: 59 additions & 0 deletions test/ng/rootScopeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,65 @@ describe('Scope', function() {
$rootScope.$digest();
expect(arrayLikelog).toEqual(['x', 'y']);
});

it('should return a new array with old values', function(){
var watchArgs;
$rootScope.$watchCollection('obj', function (newValues, oldValues) {
watchArgs = {
newValues: newValues,
oldValues: oldValues
};
});

$rootScope.obj = ['a'];
$rootScope.$digest();

expect(watchArgs.newValues).toEqual($rootScope.obj);
expect(watchArgs.oldValues).toEqual([]);

$rootScope.obj.push('b');
$rootScope.$digest();

expect(watchArgs.newValues).toEqual(['a', 'b']);
expect(watchArgs.oldValues).toEqual(['a']);
});

it('should return a new array with old values from array like objects', function(){
var watchArgs;
$rootScope.$watchCollection('arrayLikeObject', function (newValues, oldValues) {
watchArgs = {
newValues: [],
oldValues: []
};
forEach(newValues, function (element){
watchArgs.newValues.push(element.name);
});
forEach(oldValues, function (element){
watchArgs.oldValues.push(element.name);
});
});

document.body.innerHTML = "<p>" +
"<a name='x'>a</a>" +
"<a name='y'>b</a>" +
"<a name='z'>c</a>" +
"</p>";

$rootScope.arrayLikeObject = document.getElementsByTagName('a');
$rootScope.$digest();

document.body.innerHTML = "<p>" +
"<a name='x'>a</a>" +
"<a name='y'>b</a>" +
"</p>";

$rootScope.arrayLikeObject.length = 2;
$rootScope.$digest();

expect(watchArgs.newValues).toEqual(['x', 'y']);
expect(watchArgs.oldValues).toEqual(['x', 'y', 'z']);
});

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see a test proving that we handle objects properly. are you omitting this on purpose?

});


Expand Down