forked from angular/angular.js
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(ngRepeat): add tests and simplify code
This commits adds test for angular#933 and simplifies the implementation of the fix Closes angular#933
- Loading branch information
Showing
3 changed files
with
49 additions
and
53 deletions.
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
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 |
---|---|---|
|
@@ -88,7 +88,7 @@ var ngRepeatDirective = ngDirective({ | |
// We need an array of these objects since the same object can be returned from the iterator. | ||
// We expect this to be a rare case. | ||
var lastOrder = new HashQueueMap(); | ||
var indexValues = []; | ||
|
||
scope.$watch(function ngRepeatWatch(scope){ | ||
var index, length, | ||
collection = scope.$eval(rhs), | ||
|
@@ -119,18 +119,14 @@ var ngRepeatDirective = ngDirective({ | |
key = (collection === array) ? index : array[index]; | ||
value = collection[key]; | ||
|
||
// if collection is array and value is object, it can be shifted to allow for position change | ||
// if collection is array and value is not object, need to first check whether index is same to | ||
// avoid shifting wrong value | ||
// if collection is not array, need to always check index to avoid shifting wrong value | ||
if (lastOrder.peek(value)) { | ||
last = collection === array ? | ||
((isObject(value)) ? lastOrder.shift(value) : | ||
(index === lastOrder.peek(value).index ? lastOrder.shift(value) : undefined)) : | ||
(index === lastOrder.peek(value).index ? lastOrder.shift(value) : undefined); | ||
} else { | ||
last = undefined; | ||
} | ||
// if value is object, it can be shifted to allow for position change | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
IgorMinar
Author
Owner
|
||
// if is not object, need to first check whether index is same to avoid shifting wrong val | ||
last = isObject(value) | ||
? lastOrder.shift(value) | ||
: (last = lastOrder.peek(value)) && (index === last.index) | ||
? lastOrder.shift(value) | ||
: undefined; | ||
|
||
|
||
if (last) { | ||
// if we have already seen this object, then we need to reuse the | ||
|
@@ -151,12 +147,6 @@ var ngRepeatDirective = ngDirective({ | |
cursor = last.element; | ||
} | ||
} else { | ||
if (indexValues.hasOwnProperty(index) && collection !== array) { | ||
var preValue = indexValues[index]; | ||
var v = lastOrder.shift(preValue); | ||
v.element.remove(); | ||
v.scope.$destroy(); | ||
} | ||
// new item which we don't know about | ||
childScope = scope.$new(); | ||
} | ||
|
@@ -178,16 +168,10 @@ var ngRepeatDirective = ngDirective({ | |
index: index | ||
}; | ||
nextOrder.push(value, last); | ||
indexValues[index] = value; | ||
}); | ||
} | ||
} | ||
|
||
var i, l; | ||
for (i = 0, l = indexValues.length - length; i < l; i++) { | ||
indexValues.pop(); | ||
} | ||
|
||
//shrink children | ||
for (key in lastOrder) { | ||
if (lastOrder.hasOwnProperty(key)) { | ||
|
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
could you better explain the logic. I am not able to follow what is going on here.