From 20e5c18d672041fb992615710c7f11b30932dece Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Tue, 17 Jun 2014 14:13:55 +0200 Subject: [PATCH] fix(ng_repeat): fix ng_repeat not moving views for elements that have not moved Closes #1154 Closes #1155 --- lib/directive/ng_repeat.dart | 10 +++++++--- test/directive/ng_repeat_spec.dart | 9 +++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/directive/ng_repeat.dart b/lib/directive/ng_repeat.dart index 80c7cbdab..b7229d085 100644 --- a/lib/directive/ng_repeat.dart +++ b/lib/directive/ng_repeat.dart @@ -87,8 +87,7 @@ class NgRepeat { Function _generateId = (key, value, index) => value; Watch _watch; - NgRepeat(this._viewPort, this._boundViewFactory, this._scope, - this._parser, this.formatters); + NgRepeat(this._viewPort, this._boundViewFactory, this._scope, this._parser, this.formatters); set expression(value) { assert(value != null); @@ -221,9 +220,14 @@ class NgRepeat { var changeFn = changeFunctions[targetIndex]; if (changeFn == null) { rows[targetIndex] = _rows[targetIndex]; - domIndex--; // The element has not moved but `$last` and `$middle` might still need // to be updated + var childContext = _updateContext(rows[targetIndex].scope.context, targetIndex, length); + if (domIndex < 0 || leftInDom[domIndex] != targetIndex) { + _viewPort.move(rows[targetIndex].view, moveAfter: previousView); + leftInDom.remove(targetIndex); + } + domIndex--; _updateContext(rows[targetIndex].scope.context, targetIndex, length); } else { changeFn(targetIndex, previousView); diff --git a/test/directive/ng_repeat_spec.dart b/test/directive/ng_repeat_spec.dart index 160424e9d..84e64d8e3 100644 --- a/test/directive/ng_repeat_spec.dart +++ b/test/directive/ng_repeat_spec.dart @@ -466,6 +466,15 @@ main() { lis = element.querySelectorAll('li'); }); + it(r'should correctly update rows orders - gh1154', () { + scope.context['items'] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; + scope.apply(); + expect(element).toHaveText('0123456789'); + scope.context['items'] = [1, 2, 6, 7, 4, 3, 5, 8, 9, 0]; + scope.apply(); + expect(element).toHaveText('0123456789'); + }); + it(r'should preserve the order of elements', () { scope.context['items'] = [a, c, d]; scope.apply();