forked from angular/angular.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(rootScope): $watchCollection returns in oldCollection a copy of t…
…he former array data related to angular#1751 When watching arrays, $watchCollection returned the new data both in the newCollection and the oldCollection arguments of the listener
- Loading branch information
Gonzalo Ruiz de Villa
committed
Sep 4, 2013
1 parent
40c0220
commit bf69ca0
Showing
2 changed files
with
27 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -362,6 +362,7 @@ function $RootScopeProvider(){ | |
$watchCollection: function(obj, listener) { | ||
var self = this; | ||
var oldValue; | ||
var oldArray; | ||
var newValue; | ||
var changeDetected = 0; | ||
var objGetter = $parse(obj); | ||
|
@@ -371,6 +372,7 @@ function $RootScopeProvider(){ | |
|
||
function $watchCollectionWatch() { | ||
newValue = objGetter(self); | ||
oldArray = null; | ||
var newLength, key; | ||
|
||
if (!isObject(newValue)) { | ||
|
@@ -386,6 +388,8 @@ function $RootScopeProvider(){ | |
changeDetected++; | ||
} | ||
|
||
oldArray = oldValue.slice(0); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
gonzaloruizdevilla
Owner
|
||
|
||
newLength = newValue.length; | ||
|
||
if (oldLength !== newLength) { | ||
|
@@ -439,7 +443,7 @@ function $RootScopeProvider(){ | |
} | ||
|
||
function $watchCollectionAction() { | ||
listener(newValue, oldValue, self); | ||
listener(newValue, oldArray || oldValue, self); | ||
} | ||
|
||
return this.$watch($watchCollectionWatch, $watchCollectionAction); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
this is going to be a perf penalty: http://jsperf.com/slice-0
could you check if
listener.length > 1
and slice the array only then?btw what about objects/maps? slice is not going to work on those.