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

Commit

Permalink
feat(ngRepeat): add track by support
Browse files Browse the repository at this point in the history
Grab and parse the track by expression if given, and use it to
track the objects.

Closes #277
Closes #507
  • Loading branch information
teropa authored and mhevery committed Feb 6, 2014
1 parent 4f9120d commit 0756645
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
21 changes: 18 additions & 3 deletions lib/directive/ng_repeat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ class _Row {
class NgRepeatDirective extends AbstractNgRepeatDirective {
NgRepeatDirective(BlockHole blockHole,
BoundBlockFactory boundBlockFactory,
Scope scope): super(blockHole, boundBlockFactory, scope);
Parser parser,
Scope scope): super(blockHole, boundBlockFactory, parser, scope);
get _shalow => false;
}

Expand Down Expand Up @@ -112,8 +113,9 @@ class NgRepeatDirective extends AbstractNgRepeatDirective {
class NgShalowRepeatDirective extends AbstractNgRepeatDirective {
NgShalowRepeatDirective(BlockHole blockHole,
BoundBlockFactory boundBlockFactory,
Parser parser,
Scope scope)
: super(blockHole, boundBlockFactory, scope);
: super(blockHole, boundBlockFactory, parser, scope);
get _shalow => true;
}

Expand All @@ -123,6 +125,7 @@ abstract class AbstractNgRepeatDirective {

final BlockHole _blockHole;
final BoundBlockFactory _boundBlockFactory;
final Parser _parser;
final Scope _scope;

String _expression;
Expand All @@ -134,7 +137,7 @@ abstract class AbstractNgRepeatDirective {
Function _removeWatch = () => null;
Iterable _lastCollection;

AbstractNgRepeatDirective(this._blockHole, this._boundBlockFactory, this._scope);
AbstractNgRepeatDirective(this._blockHole, this._boundBlockFactory, this._parser, this._scope);

get _shalow;

Expand All @@ -147,6 +150,18 @@ abstract class AbstractNgRepeatDirective {
"in _collection_[ track by _id_]' but got '$_expression'.";
}
_listExpr = match.group(2);
var trackByExpr = match.group(3);
if (trackByExpr != null) {
Expression trackBy = _parser(trackByExpr);
_trackByIdFn = ((key, value, index) {
Map<String, Object> trackByLocals = new Map<String, Object>();
if (_keyIdentifier != null) trackByLocals[_keyIdentifier] = key;
trackByLocals[_valueIdentifier] = value;
trackByLocals[r'$index'] = index;
trackByLocals[r'$id'] = (obj) => obj;
return relaxFnArgs(trackBy.eval)(new ScopeLocals(_scope, trackByLocals));
});
}
var assignExpr = match.group(1);
match = _LHS_SYNTAX.firstMatch(assignExpr);
if (match == null) {
Expand Down
2 changes: 1 addition & 1 deletion test/directive/ng_repeat_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ main() {
});


xit(r'should iterate over an array of primitives', () {
it(r'should iterate over an array of primitives', () {
element = $compile(
r'<ul>' +
r'<li ng-repeat="item in items track by $index">{{item}};</li>' +
Expand Down

0 comments on commit 0756645

Please sign in to comment.