Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix($parse): Make sure ES6 object computed property to be watched #15637

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions src/ng/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,13 @@ function findConstantAndWatchExpressions(ast, $filter) {
if (!property.value.constant) {
argsToWatch.push.apply(argsToWatch, property.value.toWatch);
}
if (property.computed) {
findConstantAndWatchExpressions(property.key, $filter);
if (!property.key.constant) {
argsToWatch.push.apply(argsToWatch, property.key.toWatch);
}
}

});
ast.constant = allConstants;
ast.toWatch = argsToWatch;
Expand Down
35 changes: 35 additions & 0 deletions test/ng/parseSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3868,6 +3868,41 @@ describe('parser', function() {
scope.$digest();
expect(objB.value).toBe(scope.input);
}));

it('should watch ES6 object computed property changes', function() {
var count = 0;
var values = [];
var firstValue = {'undefined': true};
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't seem necessary. I think it's better to inline the object.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gkalpak fixed.


scope.$watch('{[a]: true}', function(val, oldVal) {
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Remove unused arg.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gkalpak fixed.

count++;
values.push(val);
}, true);

scope.$digest();
expect(count).toBe(1);
expect(values[0]).toEqual(firstValue);

scope.$digest();
expect(count).toBe(1);
expect(values[0]).toEqual(firstValue);

scope.a = true;
scope.$digest();
expect(count).toBe(2);
expect(values[1]).toEqual({'true': true});

scope.a = 'abc';
scope.$digest();
expect(count).toBe(3);
expect(values[2]).toEqual({'abc': true});

scope.a = undefined;
scope.$digest();
expect(count).toBe(4);
expect(values[3]).toEqual({'undefined': true});
});

});

describe('locals', function() {
Expand Down