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

Commit db662f5

Browse files
gkalpakSomeKittens
authored andcommitted
fix($watch): don't break
Due to a change introduced in AngularJS `v1.5.0` (angular/angular.js@1c6edd4), the `watchAction` callback is called without a context (i.e. `this === undefined`). This caused an error while trying to access `this.$id`. Fixes angular/batarang#285
1 parent 134f88c commit db662f5

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

dist/hint.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1501,7 +1501,7 @@ function decorateRootScope($delegate, $parse) {
15011501
var end = perf.now();
15021502
_digestEvents.push({
15031503
eventType: 'scope:reaction',
1504-
id: this.$id,
1504+
id: scopeId,
15051505
watch: watchStr,
15061506
time: end - start
15071507
});

src/modules/scopes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ function decorateRootScope($delegate, $parse) {
142142
var end = perf.now();
143143
_digestEvents.push({
144144
eventType: 'scope:reaction',
145-
id: this.$id,
145+
id: scopeId,
146146
watch: watchStr,
147147
time: end - start
148148
});

test/scopes.spec.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,25 @@ describe('ngHintScopes', function() {
5252
checkMostRecentCall(2, expectedEvent);
5353
});
5454

55+
it('should not break when watchAction is called without context', function() {
56+
var _watch = scope.$watch;
57+
scope.$watch = function(watchExpression, watchAction) {
58+
var contextlessWatchAction = watchAction.bind(undefined);
59+
return _watch.call(scope, watchExpression, contextlessWatchAction);
60+
};
61+
62+
scope.$watch('a.b', function(newValue) {});
63+
expect(hint.emit.calls.count()).toBe(0);
64+
65+
scope.$digest();
66+
checkMostRecentCall(1, {
67+
eventType: 'scope:reaction',
68+
id: scope.$id,
69+
watch: 'a.b',
70+
time: null
71+
}, 1);
72+
});
73+
5574
if (angular.version.minor >= 3) {
5675
it('should not run perf timers for one time bind expressions passed to watch', function() {
5776
var calls = hint.emit.calls;
@@ -85,9 +104,9 @@ describe('ngHintScopes', function() {
85104

86105
// the event's time property is set to null before comparison with expectedEvent, so callers
87106
// should set the time property on expectedEvent to null as well
88-
function checkMostRecentCall(expectedCount, expectedEvent){
107+
function checkMostRecentCall(expectedCount, expectedEvent, eventIdx){
89108
var calls = hint.emit.calls;
90-
var evt = calls.mostRecent().args[1].events[0];
109+
var evt = calls.mostRecent().args[1].events[eventIdx || 0];
91110
expect(calls.count()).toBe(expectedCount);
92111
expect(evt.time).toEqual(jasmine.any(Number));
93112
evt.time = null;

0 commit comments

Comments
 (0)