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

Commit 1b17dfa

Browse files
committed
fix(ngRepeat): support mostly-stable repeating for primitives
I'm reverting changes that were originally done to ngRepeat to fix #933, because these are now not necessary after the previous changes to keep ngModel always synced with the DOM.
1 parent e6d9bea commit 1b17dfa

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

src/ng/directive/ngRepeat.js

+1-8
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,7 @@ var ngRepeatDirective = ngDirective({
119119
key = (collection === array) ? index : array[index];
120120
value = collection[key];
121121

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-
122+
last = lastOrder.shift(value);
130123

131124
if (last) {
132125
// if we have already seen this object, then we need to reuse the

test/ng/directive/ngRepeatSpec.js

+18
Original file line numberDiff line numberDiff line change
@@ -431,5 +431,23 @@ describe('ngRepeat', function() {
431431
expect(newElements[1]).toEqual(lis[1]);
432432
expect(newElements[2]).toEqual(lis[0]);
433433
});
434+
435+
436+
it('should reuse elements even when model is composed of primitives', function() {
437+
// rebuilding repeater from scratch can be expensive, we should try to avoid it even for
438+
// model that is composed of primitives.
439+
440+
scope.items = ['hello', 'cau', 'ahoj'];
441+
scope.$digest();
442+
lis = element.find('li');
443+
444+
scope.items = ['ahoj', 'hello', 'cau'];
445+
scope.$digest();
446+
var newLis = element.find('li');
447+
expect(newLis.length).toEqual(3);
448+
expect(newLis[0]).toEqual(lis[2]);
449+
expect(newLis[1]).toEqual(lis[0]);
450+
expect(newLis[2]).toEqual(lis[1]);
451+
});
434452
});
435453
});

0 commit comments

Comments
 (0)