Skip to content

Commit

Permalink
fix(ngRepeat): support mostly-stable repeating for primitives
Browse files Browse the repository at this point in the history
I'm reverting changes that were originally done to ngRepeat to fix angular#933,
because these are now not necessary after the previous changes to keep
ngModel always synced with the DOM.
  • Loading branch information
IgorMinar committed Nov 23, 2012
1 parent ab1f1c8 commit 2f34fe8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
9 changes: 1 addition & 8 deletions src/ng/directive/ngRepeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,7 @@ var ngRepeatDirective = ngDirective({
key = (collection === array) ? index : array[index];
value = collection[key];

// 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;

last = lastOrder.shift(value);

if (last) {
// if we have already seen this object, then we need to reuse the
Expand Down
18 changes: 18 additions & 0 deletions test/ng/directive/ngRepeatSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,5 +431,23 @@ describe('ngRepeat', function() {
expect(newElements[1]).toEqual(lis[1]);
expect(newElements[2]).toEqual(lis[0]);
});


it('should reuse elements even when model is composed of primitives', function() {
// rebuilding repeater from scratch can be expensive, we should try to avoid it even for
// model that is composed of primitives.

scope.items = ['hello', 'cau', 'ahoj'];
scope.$digest();
lis = element.find('li');

scope.items = ['ahoj', 'hello', 'cau'];
scope.$digest();
var newLis = element.find('li');
expect(newLis.length).toEqual(3);
expect(newLis[0]).toEqual(lis[2]);
expect(newLis[1]).toEqual(lis[0]);
expect(newLis[2]).toEqual(lis[1]);
});
});
});

0 comments on commit 2f34fe8

Please sign in to comment.