The framework supports capture and bubble phases for the propagation of component events. These phases are similar to DOM handling patterns and provide an opportunity for interested components to interact with an event and potentially control the behavior for subsequent handlers. The capture phase executes before the bubble phase.
By default, every parent in the containment hierarchy can’t handle an event during the capture and bubble phases. Instead, the event propagates to every owner in the containment hierarchy.
A component’s owner is the component that is responsible for its creation. For declaratively created components, the owner is the outermost component containing the markup that references the component firing the event. For programmatically created components, the owner component is the component that invoked $A.createComponent to create it.
The same rules apply for the capture phase, although the direction of event propagation (down) is the opposite of the bubble phase (up).
Confused? It makes more sense when you look at an example in the bubbling phase.
c:owner contains c:container, which in turn contains c:eventSource.
<!--c:owner--> <aura:component> <c:container> <c:eventSource /> </c:container> </aura:component>
If c:eventSource fires an event, it can handle the event itself. The event then bubbles up the containment hierarchy.
c:container contains c:eventSource but it’s not the owner because it’s not the outermost component in the markup, so it can’t handle the bubbled event.
c:owner is the owner because c:container is in its markup. c:owner can handle the event.
The default behavior doesn’t allow an event to be handled by every parent in the containment hierarchy. Some components contain other components but aren’t the owner of those components. These components are known as container components. In the example, c:container is a container component because it’s not the owner for c:eventSource. By default, c:container can’t handle events fired by c:eventSource.
A container component has a facet attribute whose type is Aura.Component[], such as the default body attribute. The container component includes those components in its definition using an expression, such as {!v.body}. The container component isn’t the owner of the components rendered with that expression.
To allow a container component to handle the event, add includeFacets="true" to the <aura:handler> tag of the container component. For example, adding includeFacets="true" to the handler in the container component, c:container, enables it to handle the component event bubbled from c:eventSource.
<aura:handler name="bubblingEvent" event="c:compEvent" action="{!c.handleBubbling}" includeFacets="true" />
A component that fires a component event registers that it fires the event by using the <aura:registerEvent> tag.
<aura:component> <aura:registerEvent name="compEvent" type="c:compEvent" /> </aura:component>
A component handling the event in the bubble phase uses the <aura:handler> tag to assign a handling action in its client-side controller.
<aura:component> <aura:handler name="compEvent" event="c:compEvent" action="{!c.handleBubbling}"/> </aura:component>
A component handling the event in the capture phase uses the <aura:handler> tag to assign a handling action in its client-side controller.
<aura:component> <aura:handler name="compEvent" event="c:compEvent" action="{!c.handleCapture}" phase="capture" /> </aura:component>
The default handling phase for component events is bubble if no phase attribute is set.
Use the stopPropagation() method in the Event object to stop the event propagating to other components.
Use event.pause() to pause event handling and propagation until event.resume() is called. This flow-control mechanism is useful for any decision that depends on the response from the execution of asynchronous code. For example, you might make a decision about event propagation based on the response from an asynchronous call to native mobile code.
You can call pause() or resume() in the capture or bubble phases.
Let’s look at an example so you can play around with it yourself.
<!--c:eventBubblingParent--> <aura:component> <c:eventBubblingChild> <c:eventBubblingGrandchild /> </c:eventBubblingChild> </aura:component>
First, we define a simple component event.
<!--c:compEvent--> <aura:event type="COMPONENT"> <!--simple event with no attributes--> </aura:event>
c:eventBubblingEmitter is the component that fires c:compEvent.
<!--c:eventBubblingEmitter--> <aura:component> <aura:registerEvent name="bubblingEvent" type="c:compEvent" /> <lightning:button onclick="{!c.fireEvent}" label="Start Bubbling"/> </aura:component>
Here’s the controller for c:eventBubblingEmitter. When you press the button, it fires the bubblingEvent event registered in the markup.
/*eventBubblingEmitterController.js*/ { fireEvent : function(cmp) { var cmpEvent = cmp.getEvent("bubblingEvent"); cmpEvent.fire(); } }
c:eventBubblingGrandchild contains c:eventBubblingEmitter and uses <aura:handler> to assign a handler for the event.
<!--c:eventBubblingGrandchild--> <aura:component> <aura:handler name="bubblingEvent" event="c:compEvent" action="{!c.handleBubbling}"/> <div class="grandchild"> <c:eventBubblingEmitter /> </div> </aura:component>
Here’s the controller for c:eventBubblingGrandchild.
/*eventBubblingGrandchildController.js*/ { handleBubbling : function(component, event) { console.log("Grandchild handler for " + event.getName()); } }
The controller logs the event name when the handler is called.
Here’s the markup for c:eventBubblingChild. We will pass c:eventBubblingGrandchild in as the body of c:eventBubblingChild when we create c:eventBubblingParent later in this example.
<!--c:eventBubblingChild--> <aura:component> <aura:handler name="bubblingEvent" event="c:compEvent" action="{!c.handleBubbling}"/> <div class="child"> {!v.body} </div> </aura:component>
Here’s the controller for c:eventBubblingChild.
/*eventBubblingChildController.js*/ { handleBubbling : function(component, event) { console.log("Child handler for " + event.getName()); } }
c:eventBubblingParent contains c:eventBubblingChild, which in turn contains c:eventBubblingGrandchild.
<!--c:eventBubblingParent--> <aura:component> <aura:handler name="bubblingEvent" event="c:compEvent" action="{!c.handleBubbling}"/> <div class="parent"> <c:eventBubblingChild> <c:eventBubblingGrandchild /> </c:eventBubblingChild> </div> </aura:component>
Here’s the controller for c:eventBubblingParent.
/*eventBubblingParentController.js*/ { handleBubbling : function(component, event) { console.log("Parent handler for " + event.getName()); } }
Now, let’s see what happens when you run the code.
Grandchild handler for bubblingEvent Parent handler for bubblingEvent
The c:compEvent event is bubbled to c:eventBubblingGrandchild and c:eventBubblingParent as they are owners in the containment hierarchy. The event is not handled by c:eventBubblingChild as c:eventBubblingChild is in the markup for c:eventBubblingParent but it’s not an owner as it’s not the outermost component in that markup.
Now, let’s see how to stop event propagation. Edit the controller for c:eventBubblingGrandchild to stop propagation.
/*eventBubblingGrandchildController.js*/ { handleBubbling : function(component, event) { console.log("Grandchild handler for " + event.getName()); event.stopPropagation(); } }
Now, navigate to c:eventBubblingParent and click the Start Bubbling button.
Note the output in your browser’s console:
Grandchild handler for bubblingEvent
The event no longer bubbles up to the c:eventBubblingParent component.