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

feat($rootScope): implement event.stopPropagation() for $broadcast-ed events #12672

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 5 additions & 3 deletions src/ng/rootScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,7 @@ function $RootScopeProvider() {
* event propagates through the scope hierarchy, this property is set to null.
* - `name` - `{string}`: name of the event.
* - `stopPropagation` - `{function=}`: calling `stopPropagation` function will cancel
* further event propagation (available only for events that were `$emit`-ed).
* further event propagation.
* - `preventDefault` - `{function}`: calling `preventDefault` sets `defaultPrevented` flag
* to true.
* - `defaultPrevented` - `{boolean}`: true if `preventDefault` was called.
Expand Down Expand Up @@ -1232,9 +1232,11 @@ function $RootScopeProvider() {
var target = this,
current = target,
next = target,
stopPropagation = false,
event = {
name: name,
targetScope: target,
stopPropagation: function() {stopPropagation = true;},
preventDefault: function() {
event.defaultPrevented = true;
},
Expand Down Expand Up @@ -1270,8 +1272,8 @@ function $RootScopeProvider() {
// yes, this code is a bit crazy, but it works and we have tests to prove it!
// this piece should be kept in sync with the traversal in $digest
// (though it differs due to having the extra check for $$listenerCount)
if (!(next = ((current.$$listenerCount[name] && current.$$childHead) ||
(current !== target && current.$$nextSibling)))) {
if (!(next = ((current.$$listenerCount[name] && !stopPropagation && current.$$childHead) ||
(current !== target && ((stopPropagation = false) || current.$$nextSibling))))) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe it should split that complicated expression into more ifs?
But then it will be completely different than traversal in $digest

while (current !== target && !(next = current.$$nextSibling)) {
current = current.$parent;
}
Expand Down
14 changes: 14 additions & 0 deletions test/ng/rootScopeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2007,6 +2007,20 @@ describe('Scope', function() {
}));


it('should allow stopping event propagation', inject(function($rootScope) {
child2.$on('myEvent', function(event) { event.stopPropagation(); });
$rootScope.$broadcast('myEvent');
expect(log).toEqual('0>1>11>2>3>');
}));


it('should stop propagation only for one scope chain', inject(function($rootScope) {
child1.$on('myEvent', function(event) { event.stopPropagation(); });
$rootScope.$broadcast('myEvent');
expect(log).toEqual('0>1>2>21>211>22>23>3>');
}));


it('should broadcast an event from a child scope', function() {
child2.$broadcast('myEvent');
expect(log).toBe('2>21>211>22>23>');
Expand Down