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

Commit

Permalink
fix(ng-repeat): don't use iterable.length
Browse files Browse the repository at this point in the history
  • Loading branch information
chirayuk committed Mar 26, 2014
1 parent ae15983 commit cf2671a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/change_detection/change_detection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ abstract class ChangedKeyValue<K, V> extends MapKeyValue<K, V> {
abstract class CollectionChangeRecord<V> {
/** The underlying iterable object */
Iterable get iterable;
int get length;

/** A list of [CollectionItem]s which are in the iteration order. */
CollectionItem<V> get collectionHead;
Expand Down
5 changes: 5 additions & 0 deletions lib/change_detection/dirty_checking_change_detector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,8 @@ class KeyValueRecord<K, V> implements KeyValue<K, V>, AddedKeyValue<K, V>,

class _CollectionChangeRecord<V> implements CollectionChangeRecord<V> {
Iterable _iterable;
int _length;

/** Used to keep track of items during moves. */
DuplicateMap _items = new DuplicateMap();

Expand Down Expand Up @@ -836,6 +838,7 @@ class _CollectionChangeRecord<V> implements CollectionChangeRecord<V> {
}

Iterable get iterable => _iterable;
int get length => _length;

bool _check(Iterable collection) {
_reset();
Expand All @@ -849,6 +852,7 @@ class _CollectionChangeRecord<V> implements CollectionChangeRecord<V> {

if (collection is List) {
List list = collection;
_length = list.length;
for (int index = 0; index < list.length; index++) {
var item = list[index];
if (record == null || !identical(item, record.item)) {
Expand All @@ -873,6 +877,7 @@ class _CollectionChangeRecord<V> implements CollectionChangeRecord<V> {
record = record._nextRec;
index++;
}
_length = index;
}

_truncate(record);
Expand Down
2 changes: 1 addition & 1 deletion lib/directive/ng_repeat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class NgRepeatDirective {

// Computes and executes DOM changes when the item list changes
void _onChange(CollectionChangeRecord changes) {
final int length = changes.iterable.length;
final int length = changes.length;
final rows = new List<_Row>(length);
final changeFunctions = new List<Function>(length);
final removedIndexes = <int>[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -701,9 +701,11 @@ class CollectionRecordMatcher extends Matcher {

bool checkCollection(CollectionChangeRecord changeRecord, List diffs) {
var equals = true;
int count = 0;
if (collection != null) {
CollectionItem collectionItem = changeRecord.collectionHead;
for (var item in collection) {
count++;
if (collectionItem == null) {
equals = false;
diffs.add('collection too short: $item');
Expand All @@ -720,6 +722,11 @@ class CollectionRecordMatcher extends Matcher {
equals = false;
}
}
var iterableLength = changeRecord.iterable.toList().length;
if (iterableLength != count) {
diffs.add('collection length mismatched: $iterableLength != $count');
equals = false;
}
return equals;
}

Expand Down
14 changes: 14 additions & 0 deletions test/directive/ng_repeat_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ main() {
});


it(r'should set create a list of items', (Scope scope, Compiler compiler, Injector injector) {
scope.context['items'] = [];
scope.watch('1', (_, __) {
scope.context['items'].add('a');
scope.context['items'].add('b');
});
var element = es('<div><div ng-repeat="item in items">{{item}}</div></div>');
ViewFactory viewFactory = compiler(element, directives);
View view = viewFactory(injector, element);
scope.apply();
expect(element).toHaveText('ab');
});


it(r'should set create a list of items from iterable',
(Scope scope, Compiler compiler, Injector injector) {
var element = es('<div><div ng-repeat="item in items">{{item}}</div></div>');
Expand Down

0 comments on commit cf2671a

Please sign in to comment.