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

Commit 00e7e31

Browse files
committed
fix(ngRepeat): attempt to simplify and improve existing fix for #933
I'm keeping this in for future reference. The issue with this solution is that if we shift() the first item in the array, the whole repeater DOM will be rebuilt from scratch, we need to do better than that.
1 parent ff4b3e2 commit 00e7e31

File tree

2 files changed

+11
-27
lines changed

2 files changed

+11
-27
lines changed

src/apis.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,12 @@ HashQueueMap.prototype = {
9898
}
9999
}
100100
},
101-
101+
102102
/**
103103
* return the first item without deleting it
104104
*/
105105
peek: function(key) {
106-
var array = this[key = hashKey(key)];
106+
var array = this[hashKey(key)];
107107
if (array) {
108108
return array[0];
109109
}

src/ng/directive/ngRepeat.js

+9-25
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ var ngRepeatDirective = ngDirective({
8888
// We need an array of these objects since the same object can be returned from the iterator.
8989
// We expect this to be a rare case.
9090
var lastOrder = new HashQueueMap();
91-
var indexValues = [];
91+
9292
scope.$watch(function ngRepeatWatch(scope){
9393
var index, length,
9494
collection = scope.$eval(rhs),
@@ -119,18 +119,14 @@ var ngRepeatDirective = ngDirective({
119119
key = (collection === array) ? index : array[index];
120120
value = collection[key];
121121

122-
// if collection is array and value is object, it can be shifted to allow for position change
123-
// if collection is array and value is not object, need to first check whether index is same to
124-
// avoid shifting wrong value
125-
// if collection is not array, need to always check index to avoid shifting wrong value
126-
if (lastOrder.peek(value)) {
127-
last = collection === array ?
128-
((isObject(value)) ? lastOrder.shift(value) :
129-
(index === lastOrder.peek(value).index ? lastOrder.shift(value) : undefined)) :
130-
(index === lastOrder.peek(value).index ? lastOrder.shift(value) : undefined);
131-
} else {
132-
last = undefined;
133-
}
122+
// if value is object, it can be shifted to allow for position change
123+
// if is not object, need to first check whether index is same to avoid shifting wrong val
124+
last = isObject(value)
125+
? lastOrder.shift(value)
126+
: (last = lastOrder.peek(value)) && (index === last.index)
127+
? lastOrder.shift(value)
128+
: undefined;
129+
134130

135131
if (last) {
136132
// if we have already seen this object, then we need to reuse the
@@ -151,12 +147,6 @@ var ngRepeatDirective = ngDirective({
151147
cursor = last.element;
152148
}
153149
} else {
154-
if (indexValues.hasOwnProperty(index) && collection !== array) {
155-
var preValue = indexValues[index];
156-
var v = lastOrder.shift(preValue);
157-
v.element.remove();
158-
v.scope.$destroy();
159-
}
160150
// new item which we don't know about
161151
childScope = scope.$new();
162152
}
@@ -178,16 +168,10 @@ var ngRepeatDirective = ngDirective({
178168
index: index
179169
};
180170
nextOrder.push(value, last);
181-
indexValues[index] = value;
182171
});
183172
}
184173
}
185174

186-
var i, l;
187-
for (i = 0, l = indexValues.length - length; i < l; i++) {
188-
indexValues.pop();
189-
}
190-
191175
//shrink children
192176
for (key in lastOrder) {
193177
if (lastOrder.hasOwnProperty(key)) {

0 commit comments

Comments
 (0)