Skip to content

Commit 7dbf428

Browse files
committed
fixup! correctly re-create last block order when track by is an integer
1 parent fc8ac23 commit 7dbf428

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

Diff for: src/ng/directive/ngRepeat.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -485,9 +485,9 @@ var ngRepeatDirective = ['$parse', '$animate', '$compile', function($parse, $ani
485485
nextBlockOrder[index] = trackById;
486486
}
487487

488-
// setup lastBlockOrder, used to determine if block moved
488+
// Set up lastBlockOrder. Used to determine if a block moved.
489489
for (key in lastBlockMap) {
490-
lastBlockOrder.push(key);
490+
lastBlockOrder[lastBlockMap[key].index] = key;
491491
}
492492

493493
for (index = 0; index < nextLength; index++) {

Diff for: test/ng/directive/ngRepeatSpec.js

+52
Original file line numberDiff line numberDiff line change
@@ -1624,4 +1624,56 @@ describe('ngRepeat animations', function() {
16241624
expect(item.element.text()).toBe('2');
16251625
})
16261626
);
1627+
1628+
it('should maintain the order when the track by expression evaluates to an integer',
1629+
inject(function($compile, $rootScope, $animate, $document, $sniffer, $timeout) {
1630+
if (!$sniffer.transitions) return;
1631+
1632+
var item;
1633+
var ss = createMockStyleSheet($document);
1634+
1635+
var items = [
1636+
{id: 1, name: 'A'},
1637+
{id: 2, name: 'B'},
1638+
{id: 4, name: 'C'},
1639+
{id: 3, name: 'D'}
1640+
]
1641+
1642+
try {
1643+
1644+
$animate.enabled(true);
1645+
1646+
ss.addRule('.animate-me div',
1647+
'transition:1s linear all;');
1648+
1649+
element = $compile(html('<div class="animate-me">' +
1650+
'<div ng-repeat="item in items track by item.id">{{ item.name }}</div>' +
1651+
'</div>'))($rootScope);
1652+
1653+
$rootScope.items = [items[0], items[1], items[2]];
1654+
$rootScope.$digest();
1655+
expect(element.text()).toBe('ABC');
1656+
1657+
$rootScope.items.push(items[3]);
1658+
$rootScope.$digest();
1659+
1660+
expect(element.text()).toBe('ABCD'); // the original order should be preserved
1661+
$animate.flush();
1662+
$timeout.flush(1500); // 1s * 1.5 closing buffer
1663+
expect(element.text()).toBe('ABCD');
1664+
1665+
$rootScope.items = [items[0], items[1], items[3]];
1666+
$rootScope.$digest();
1667+
1668+
// The leaving item should maintain it's position until it is removed
1669+
expect(element.text()).toBe('ABCD');
1670+
$animate.flush();
1671+
$timeout.flush(1500); // 1s * 1.5 closing buffer
1672+
expect(element.text()).toBe('ABD');
1673+
1674+
} finally {
1675+
ss.destroy();
1676+
}
1677+
})
1678+
);
16271679
});

0 commit comments

Comments
 (0)