Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit abffb60

Browse files
vicbmhevery
authored andcommitted
style(ng-repeat): code cleanup
Closes #582
1 parent 3e21486 commit abffb60

File tree

1 file changed

+27
-35
lines changed

1 file changed

+27
-35
lines changed

lib/directive/ng_repeat.dart

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ class NgRepeatDirective extends AbstractNgRepeatDirective {
8484
NgRepeatDirective(BlockHole blockHole,
8585
BoundBlockFactory boundBlockFactory,
8686
Parser parser,
87-
Scope scope): super(blockHole, boundBlockFactory, parser, scope);
88-
get _shalow => false;
87+
Scope scope)
88+
: super(blockHole, boundBlockFactory, parser, scope);
8989

9090
get _shallow => false;
9191
}
@@ -104,7 +104,7 @@ class NgRepeatDirective extends AbstractNgRepeatDirective {
104104
* [length] property changes. This means that the repeater will still see
105105
* additions and deletions but not changes to the array.
106106
* * The child scopes for each item are created in the lazy mode
107-
* (see [Scope.$new]). This means the scopes are effectivly taken out of the
107+
* (see [Scope.$new]). This means the scopes are effectively taken out of the
108108
* digest cycle and will not update on changes to the model.
109109
*
110110
*/
@@ -134,7 +134,7 @@ abstract class AbstractNgRepeatDirective {
134134
String _valueIdentifier;
135135
String _keyIdentifier;
136136
String _listExpr;
137-
Map<Object, _Row> _rows = new Map<dynamic, _Row>();
137+
Map<dynamic, _Row> _rows = {};
138138
Function _trackByIdFn = (key, value, index) => value;
139139
Function _removeWatch = () => null;
140140
Iterable _lastCollection;
@@ -156,11 +156,12 @@ abstract class AbstractNgRepeatDirective {
156156
if (trackByExpr != null) {
157157
Expression trackBy = _parser(trackByExpr);
158158
_trackByIdFn = ((key, value, index) {
159-
Map<String, Object> trackByLocals = new Map<String, Object>();
159+
final trackByLocals = <String, Object>{};
160160
if (_keyIdentifier != null) trackByLocals[_keyIdentifier] = key;
161-
trackByLocals[_valueIdentifier] = value;
162-
trackByLocals[r'$index'] = index;
163-
trackByLocals[r'$id'] = (obj) => obj;
161+
trackByLocals
162+
..[_valueIdentifier] = value
163+
..[r'$index'] = index
164+
..[r'$id'] = (obj) => obj;
164165
return relaxFnArgs(trackBy.eval)(new ScopeLocals(_scope, trackByLocals));
165166
});
166167
}
@@ -180,14 +181,12 @@ abstract class AbstractNgRepeatDirective {
180181
}
181182

182183
List<_Row> _computeNewRows(Iterable collection, trackById) {
183-
List<_Row> newRowOrder = [];
184+
final newRowOrder = new List<_Row>(collection.length);
184185
// Same as lastBlockMap but it has the current state. It will become the
185186
// lastBlockMap on the next iteration.
186-
Map<dynamic, _Row> newRows = new Map<dynamic, _Row>();
187-
var arrayLength = collection.length;
187+
final newRows = <dynamic, _Row>{};
188188
// locate existing items
189-
var length = newRowOrder.length = collection.length;
190-
for (var index = 0; index < length; index++) {
189+
for (var index = 0; index < newRowOrder.length; index++) {
191190
var value = collection.elementAt(index);
192191
trackById = _trackByIdFn(index, value, index);
193192
if (_rows.containsKey(trackById)) {
@@ -198,9 +197,7 @@ abstract class AbstractNgRepeatDirective {
198197
} else if (newRows.containsKey(trackById)) {
199198
// restore lastBlockMap
200199
newRowOrder.forEach((row) {
201-
if (row != null && row.startNode != null) {
202-
_rows[row.id] = row;
203-
}
200+
if (row != null && row.startNode != null) _rows[row.id] = row;
204201
});
205202
// This is a duplicate and we need to throw an error
206203
throw "[NgErr50] ngRepeat error! Duplicates in a repeater are not "
@@ -230,13 +227,11 @@ abstract class AbstractNgRepeatDirective {
230227
arrayChange = _lastCollection != collection;
231228

232229
if (arrayChange) _lastCollection = collection;
233-
if (collection is! Iterable) {
234-
collection = [];
235-
}
230+
if (collection is! Iterable) collection = [];
236231

237232
List<_Row> newRowOrder = _computeNewRows(collection, trackById);
238233

239-
for (var index = 0, length = collection.length; index < length; index++) {
234+
for (var index = 0; index < collection.length; index++) {
240235
var value = collection.elementAt(index);
241236
_Row row = newRowOrder[index];
242237

@@ -250,10 +245,8 @@ abstract class AbstractNgRepeatDirective {
250245
nextNode = nextNode.nextNode;
251246
} while(nextNode != null);
252247

253-
if (row.startNode != nextNode) {
254-
// existing item which got moved
255-
row.block.moveAfter(cursor);
256-
}
248+
// existing item which got moved
249+
if (row.startNode != nextNode) row.block.moveAfter(cursor);
257250
previousNode = row.endNode;
258251
} else {
259252
// new item which we don't know about
@@ -264,20 +257,19 @@ abstract class AbstractNgRepeatDirective {
264257
childScope[_valueIdentifier] = value;
265258
childScope.$dirty();
266259
}
267-
childScope[r'$index'] = index;
268-
childScope[r'$first'] = (index == 0);
269-
childScope[r'$last'] = (index == (collection.length - 1));
270-
childScope[r'$middle'] = !(childScope.$first || childScope.$last);
271-
childScope[r'$odd'] = index & 1 == 1;
272-
childScope[r'$even'] = index & 1 == 0;
273-
if (arrayChange && _shalow) {
274-
childScope.$dirty();
275-
}
260+
childScope
261+
..[r'$index'] = index
262+
..[r'$first'] = (index == 0)
263+
..[r'$last'] = (index == (collection.length - 1))
264+
..[r'$middle'] = !(childScope.$first || childScope.$last)
265+
..[r'$odd'] = index & 1 == 1
266+
..[r'$even'] = index & 1 == 0;
267+
if (arrayChange && _shallow) childScope.$dirty();
276268

277269
if (row.startNode == null) {
278-
_rows[row.id] = row;
279270
var block = _boundBlockFactory(childScope);
280-
row..block = block
271+
_rows[row.id] = row
272+
..block = block
281273
..scope = childScope
282274
..elements = block.elements
283275
..startNode = row.elements[0]

0 commit comments

Comments
 (0)