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

fix(Scope): $broadcast and $emit should set event.currentScope to null #7523

Closed
wants to merge 1 commit into from

Conversation

IgorMinar
Copy link
Contributor

When a event is finished propagating through Scope hierarchy the event's currentScope property
should be reset to null to avoid accidental use of this property in asynchronous event handlers.

In the previous code, the event's property would contain a reference to the last Scope instance that
was visited during the traversal, which is unlikely what the code trying to grab scope reference expects.

BREAKING CHANGE: $broadcast and $emit will now reset the currentScope property of the event to
null once the event finished propagating. If any code depends on asynchronously accessing thei
currentScope property, it should be migrated to use targetScope instead. All of these cases
should be considered programming bugs.

Closes #7445"

@mary-poppins
Copy link

Thanks for the PR! Please check the items below to help us merge this faster. See the contributing docs for more information.

  • Uses the issue template (#7523)

If you need to make changes to your pull request, you can update the commit with git commit --amend.
Then, update the pull request with git push -f.

Thanks again for your help!

When a event is finished propagating through Scope hierarchy the event's `currentScope` property
should be reset to `null` to avoid accidental use of this property in asynchronous event handlers.

In the previous code, the event's property would contain a reference to the last Scope instance that
was visited during the traversal, which is unlikely what the code trying to grab scope reference expects.

BREAKING CHANGE: $broadcast and $emit will now reset the `currentScope` property of the event to
null once the event finished propagating. If any code depends on asynchronously accessing thei
`currentScope` property, it should be migrated to use `targetScope` instead. All of these cases
should be considered programming bugs.

Closes angular#7445
@caitp
Copy link
Contributor

caitp commented May 20, 2014

I think this should be paired with a docs fix about not expecting the event object to look the same in a different turn of the event loop. Just a minor note about that

eventFired = true;
});
grandChild.$emit('myEvent');
expect(eventFired).toBeDefined();
Copy link
Contributor

Choose a reason for hiding this comment

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

it's defined to begin with, should be toBe('true')

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good catch. I was refactoring old test and left in the old assertion.

fixed.

@caitp
Copy link
Contributor

caitp commented May 20, 2014

Looks okay to me with those minor nits addressed, and a note in the docs added regarding expectations of asynchronous behaviour

@ncuillery
Copy link
Contributor

👍 for a doc addition.

Glad to be helpful. It was a pleasure to discover AngularJS under the hood (and it looks great!).

@IgorMinar IgorMinar closed this in 82f45ae May 21, 2014
@IgorMinar
Copy link
Contributor Author

landed. thanks @ncuillery and @caitp!

RichardLitt pushed a commit to RichardLitt/angular.js that referenced this pull request May 24, 2014
When a event is finished propagating through Scope hierarchy the event's `currentScope` property
should be reset to `null` to avoid accidental use of this property in asynchronous event handlers.

In the previous code, the event's property would contain a reference to the last Scope instance that
was visited during the traversal, which is unlikely what the code trying to grab scope reference expects.

BREAKING CHANGE: $broadcast and $emit will now reset the `currentScope` property of the event to
null once the event finished propagating. If any code depends on asynchronously accessing thei
`currentScope` property, it should be migrated to use `targetScope` instead. All of these cases
should be considered programming bugs.

Closes angular#7445
Closes angular#7523
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants