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

fix(WatchGroup): Handle watching on elements of an array correctly. #1067

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/change_detection/ast_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,11 @@ _operation_logical_and(left, right) => toBool(left) && toBool(right);
_operation_logical_or(left, right) => toBool(left) || toBool(right);

_operation_ternary(condition, yes, no) => toBool(condition) ? yes : no;
_operation_bracket(obj, key) => obj == null ? null : obj[key];
_operation_bracket(obj, key) {
if (obj is! List || (key is int && key >= 0 && key < obj.length)) {
return obj[key];
}
}

class ArrayFn extends FunctionApply {
// TODO(misko): figure out why do we need to make a copy?
Expand Down
49 changes: 47 additions & 2 deletions test/directive/ng_repeat_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,6 @@ main() {
expect(element.text).toEqual('misko:0|shyam:1|frodo:2|');
});


it(r'should expose iterator position as $first, $middle and $last when iterating over arrays',
() {
element = compile(
Expand Down Expand Up @@ -401,6 +400,53 @@ main() {
expect(element.text).toEqual('a|b|Xc|d|X');
});

describe('nested watching', () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps rephrase:

From: should not error when the first item is being watched and it is removed
To: should not error when the first watched item is removed

it('should not error when the first watched item is removed', () {
element = compile(
'<ul>'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could make all of these raw string, (i.e. r'foo' instead of 'foo') and then you don't need to escape the "$" for "$index" in the string.

' <li ng-repeat="i in items">'
r' <input ng-model="items[$index]">'
' </li>'
'</ul>');
scope.context['items'] = ['misko', 'shyam', 'frodo'];
scope.apply();
expect(element.children.length).toEqual(3);
scope.context['items'].remove('misko');
scope.apply();
expect(element.children.length).toEqual(2);
});

it('should not error when the last watched item is removed', () {
element = compile(
'<ul>'
' <li ng-repeat="i in items">'
r' <input ng-model="items[$index]">'
' </li>'
'</ul>');
scope.context['items'] = ['misko', 'shyam', 'frodo'];
scope.apply();
expect(element.children.length).toEqual(3);
scope.context['items'].remove('frodo');
scope.apply();
expect(element.children.length).toEqual(2);
});

it('should not error when multiple watched items are removed at the same time', () {
element = compile(
'<ul>'
' <li ng-repeat="i in items">'
r' <input ng-model="items[$index]">'
' </li>'
'</ul>');
scope.context['items'] = ['misko', 'shyam', 'frodo', 'igor'];
scope.apply();
expect(element.children.length).toEqual(4);
scope.context['items'].remove('shyam');
scope.context['items'].remove('frodo');
scope.apply();
expect(element.children.length).toEqual(2);
});
});

describe('stability', () {
var a, b, c, d, lis;
Expand All @@ -420,7 +466,6 @@ main() {
lis = element.querySelectorAll('li');
});


it(r'should preserve the order of elements', () {
scope.context['items'] = [a, c, d];
scope.apply();
Expand Down