aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorZhenbo Zhang2012-05-09 00:00:38 +0300
committerMisko Hevery2012-09-06 16:06:21 -0700
commitf2b7fffdc0b0ad80cebb24f5fea743e9e4a439d5 (patch)
tree8312908d27009db361ac831e1ff084724e28ffe9 /src
parent42c38b29f7dcb3327fe58e630b8e2973676989e0 (diff)
downloadangular.js-f2b7fffdc0b0ad80cebb24f5fea743e9e4a439d5.tar.bz2
fix(ngRepeat): now works with primitive types
closes #933
Diffstat (limited to 'src')
-rw-r--r--src/apis.js10
-rw-r--r--src/ng/directive/ngRepeat.js28
2 files changed, 37 insertions, 1 deletions
diff --git a/src/apis.js b/src/apis.js
index 7b470802..098d9cdd 100644
--- a/src/apis.js
+++ b/src/apis.js
@@ -97,5 +97,15 @@ HashQueueMap.prototype = {
return array.shift();
}
}
+ },
+
+ /**
+ * return the first item without deleting it
+ */
+ peek: function(key) {
+ var array = this[key = hashKey(key)];
+ if (array) {
+ return array[0];
+ }
}
};
diff --git a/src/ng/directive/ngRepeat.js b/src/ng/directive/ngRepeat.js
index 6dd74ef5..a47b7abc 100644
--- a/src/ng/directive/ngRepeat.js
+++ b/src/ng/directive/ngRepeat.js
@@ -88,6 +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(scope){
var index, length,
collection = scope.$eval(rhs),
@@ -117,7 +118,20 @@ var ngRepeatDirective = ngDirective({
for (index = 0, length = array.length; index < length; index++) {
key = (collection === array) ? index : array[index];
value = collection[key];
- last = lastOrder.shift(value);
+
+ // 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 (last) {
// if we have already seen this object, then we need to reuse the
// associated scope/element
@@ -137,6 +151,12 @@ 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();
}
@@ -158,10 +178,16 @@ 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)) {