@@ -132,87 +132,90 @@ class NgRepeat {
132
132
_watch = _scope.watch (
133
133
_listExpr,
134
134
(changes, _) {
135
- if (changes is CollectionChangeRecord && changes != null ) {
136
- _onCollectionChange (changes);
137
- } else if (_rows != null ) {
138
- _rows.forEach ((row) {
139
- row.scope.destroy ();
140
- _viewPort.remove (row.view);
141
- });
142
- _rows = null ;
143
- }
135
+ _onChange ((changes is CollectionChangeRecord ) ? changes : null );
144
136
},
145
137
collection: true ,
146
138
formatters: formatters
147
139
);
148
140
}
149
141
150
142
// Computes and executes DOM changes when the item list changes
151
- void _onCollectionChange (CollectionChangeRecord changes) {
152
- final int length = changes.length;
143
+ void _onChange (CollectionChangeRecord changes) {
144
+ final iterable = (changes == null ) ? const [] : changes.iterable;
145
+ final int length = (changes == null ) ? 0 : changes.length;
153
146
final rows = new List <_Row >(length);
154
147
final changeFunctions = new List <Function >(length);
155
148
final removedIndexes = < int > [];
156
149
final int domLength = _rows == null ? 0 : _rows.length;
157
150
final leftInDom = new List .generate (domLength, (i) => domLength - 1 - i);
158
151
var domIndex;
159
152
160
- Function addFn, moveFn, removeFn;
153
+ var addRow = (int index, value, View previousView) {
154
+ var childContext = _updateContext (new PrototypeMap (_scope.context), index,
155
+ length)..[_valueIdentifier] = value;
156
+ var childScope = _scope.createChild (childContext);
157
+ var view = _boundViewFactory (childScope);
158
+ var nodes = view.nodes;
159
+ rows[index] = new _Row (_generateId (index, value, index))
160
+ ..view = view
161
+ ..scope = childScope
162
+ ..nodes = nodes
163
+ ..startNode = nodes.first
164
+ ..endNode = nodes.last;
165
+ _viewPort.insert (view, insertAfter: previousView);
166
+ };
161
167
168
+ // todo(vicb) refactor once GH-774 gets fixed
162
169
if (_rows == null ) {
163
- addFn = changes.forEachItem;
164
- moveFn = (_) {};
165
- removeFn = (_) {};
170
+ _rows = new List <_Row >(length);
171
+ for (var i = 0 ; i < length; i++ ) {
172
+ changeFunctions[i] = (index, previousView) {
173
+ addRow (index, iterable.elementAt (i), previousView);
174
+ };
175
+ }
166
176
} else {
167
- addFn = changes.forEachAddition;
168
- moveFn = changes.forEachMove;
169
- removeFn = changes.forEachRemoval;
170
- }
171
-
172
- removeFn ((CollectionChangeItem removal) {
173
- var index = removal.previousIndex;
174
- var row = _rows[index];
175
- row.scope.destroy ();
176
- _viewPort.remove (row.view);
177
- leftInDom.removeAt (domLength - 1 - index);
178
- });
177
+ if (changes == null ) {
178
+ _rows.forEach ((row) {
179
+ row.scope.destroy ();
180
+ _viewPort.remove (row.view);
181
+ });
182
+ leftInDom.clear ();
183
+ } else {
184
+ changes.forEachRemoval ((CollectionChangeItem removal) {
185
+ var index = removal.previousIndex;
186
+ var row = _rows[index];
187
+ row.scope.destroy ();
188
+ _viewPort.remove (row.view);
189
+ leftInDom.removeAt (domLength - 1 - index);
190
+ });
179
191
180
- addFn ((CollectionChangeItem addition) {
181
- changeFunctions[addition.currentIndex] = (index, previousView) {
182
- var childContext = _updateContext (new PrototypeMap (_scope.context), index,length)
183
- ..[_valueIdentifier] = addition.item;
184
- var childScope = _scope.createChild (childContext);
185
- var view = _boundViewFactory (childScope);
186
- var nodes = view.nodes;
187
- rows[index] = new _Row (_generateId (index, addition.item, index))
188
- ..view = view
189
- ..scope = childScope
190
- ..nodes = nodes
191
- ..startNode = nodes.first
192
- ..endNode = nodes.last;
193
- _viewPort.insert (view, insertAfter: previousView);
194
- };
195
- });
192
+ changes.forEachAddition ((CollectionChangeItem addition) {
193
+ changeFunctions[addition.currentIndex] = (index, previousView) {
194
+ addRow (index, addition.item, previousView);
195
+ };
196
+ });
196
197
197
- moveFn ((CollectionChangeItem move) {
198
- var previousIndex = move.previousIndex;
199
- var value = move.item;
200
- changeFunctions[move.currentIndex] = (index, previousView) {
201
- var previousRow = _rows[previousIndex];
202
- var childScope = previousRow.scope;
203
- var childContext = _updateContext (childScope.context, index, length);
204
- if (! identical (childScope.context[_valueIdentifier], value)) {
205
- childContext[_valueIdentifier] = value;
206
- }
207
- rows[index] = _rows[previousIndex];
208
- // Only move the DOM node when required
209
- if (domIndex < 0 || leftInDom[domIndex] != previousIndex) {
210
- _viewPort.move (previousRow.view, moveAfter: previousView);
211
- leftInDom.remove (previousIndex);
212
- }
213
- domIndex-- ;
214
- };
215
- });
198
+ changes.forEachMove ((CollectionChangeItem move) {
199
+ var previousIndex = move.previousIndex;
200
+ var value = move.item;
201
+ changeFunctions[move.currentIndex] = (index, previousView) {
202
+ var previousRow = _rows[previousIndex];
203
+ var childScope = previousRow.scope;
204
+ var childContext = _updateContext (childScope.context, index, length);
205
+ if (! identical (childScope.context[_valueIdentifier], value)) {
206
+ childContext[_valueIdentifier] = value;
207
+ }
208
+ rows[index] = _rows[previousIndex];
209
+ // Only move the DOM node when required
210
+ if (domIndex < 0 || leftInDom[domIndex] != previousIndex) {
211
+ _viewPort.move (previousRow.view, moveAfter: previousView);
212
+ leftInDom.remove (previousIndex);
213
+ }
214
+ domIndex-- ;
215
+ };
216
+ });
217
+ }
218
+ }
216
219
217
220
var previousView = null ;
218
221
domIndex = leftInDom.length - 1 ;
0 commit comments