aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorIgor Minar2012-11-26 20:25:38 +0100
committerIgor Minar2012-11-26 20:31:54 +0100
commitd644dcfa52cba0cee3c6fa8d03e1654cdb654e53 (patch)
tree026ae156741ea6994785be46dece6db862396c3d /src
parente7d37ee45ac48b0ff2a3d43f7236d316be6c241e (diff)
downloadangular.js-d644dcfa52cba0cee3c6fa8d03e1654cdb654e53.tar.bz2
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.
Diffstat (limited to 'src')
-rw-r--r--src/apis.js4
-rw-r--r--src/ng/directive/ngRepeat.js34
2 files changed, 11 insertions, 27 deletions
diff --git a/src/apis.js b/src/apis.js
index 098d9cdd..0e94e2a5 100644
--- a/src/apis.js
+++ b/src/apis.js
@@ -98,12 +98,12 @@ HashQueueMap.prototype = {
}
}
},
-
+
/**
* return the first item without deleting it
*/
peek: function(key) {
- var array = this[key = hashKey(key)];
+ var array = this[hashKey(key)];
if (array) {
return array[0];
}
diff --git a/src/ng/directive/ngRepeat.js b/src/ng/directive/ngRepeat.js
index 021845fa..8c934b76 100644
--- a/src/ng/directive/ngRepeat.js
+++ b/src/ng/directive/ngRepeat.js
@@ -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
+ // 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)) {