Skip to content

Commit 40302fb

Browse files
committed
fix(ng_repeat): fix ng_repeat not moving views for elements that have not moved
Closes dart-archive#1154
1 parent 1c7c0ba commit 40302fb

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

lib/directive/ng_repeat.dart

+10-3
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ class NgRepeat {
8787
Function _generateId = (key, value, index) => value;
8888
Watch _watch;
8989

90-
NgRepeat(this._viewPort, this._boundViewFactory, this._scope,
91-
this._parser, this.formatters);
90+
NgRepeat(this._viewPort, this._boundViewFactory, this._scope, this._parser, this.formatters);
9291

9392
set expression(value) {
9493
assert(value != null);
@@ -154,6 +153,9 @@ class NgRepeat {
154153
final changeFunctions = new List<Function>(length);
155154
final removedIndexes = <int>[];
156155
final int domLength = _rows == null ? 0 : _rows.length;
156+
// DOM indexes of the former rows not yet handled in the
157+
// They are stored in reverse order to simplify the code in removeFn which is called with
158+
// indexes in ascending order
157159
final leftInDom = new List.generate(domLength, (i) => domLength - 1 - i);
158160
var domIndex;
159161

@@ -220,9 +222,14 @@ class NgRepeat {
220222
var changeFn = changeFunctions[targetIndex];
221223
if (changeFn == null) {
222224
rows[targetIndex] = _rows[targetIndex];
223-
domIndex--;
224225
// The element has not moved but `$last` and `$middle` might still need
225226
// to be updated
227+
var childContext = _updateContext(rows[targetIndex].scope.context, targetIndex, length);
228+
if (domIndex < 0 || leftInDom[domIndex] != targetIndex) {
229+
_viewPort.move(rows[targetIndex].view, moveAfter: previousView);
230+
leftInDom.remove(targetIndex);
231+
}
232+
domIndex--;
226233
_updateContext(rows[targetIndex].scope.context, targetIndex, length);
227234
} else {
228235
changeFn(targetIndex, previousView);

test/directive/ng_repeat_spec.dart

+8
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,14 @@ main() {
420420
lis = element.querySelectorAll('li');
421421
});
422422

423+
it(r'should correctly update rows orders - gh1154', () {
424+
scope.context['items'] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
425+
scope.apply();
426+
expect(element).toHaveText('0123456789');
427+
scope.context['items'] = [1, 2, 6, 7, 4, 3, 5, 8, 9, 0];
428+
scope.apply();
429+
expect(element).toHaveText('0123456789');
430+
});
423431

424432
it(r'should preserve the order of elements', () {
425433
scope.context['items'] = [a, c, d];

0 commit comments

Comments
 (0)