Understanding Events in Aura
- Get link
- X
- Other Apps
Aura events in Salesforce Lightning are crucial for enabling communication between components. There are two primary types of events in Aura: Application Events and Component Events. Both types follow propagation patterns similar to DOM event handling, such as capture and bubble phases. Here’s a breakdown of how these events function and propagate within the Aura framework.
1. Application Events
Application events follow a publish-subscribe model, meaning that once an event is fired, any component that has registered a handler for that event will be notified. The propagation of application events follows three phases: capture, bubble, and default.
Phases of Application Event Propagation:
Event Fired: An application event is fired by a component. This component is known as the source component.
Capture Phase: The framework begins executing the capture phase, starting from the application root and moving down to the source component. Each component along this path has the chance to handle the event. The event’s propagation can be stopped during this phase by calling
event.stopPropagation()
, which will prevent the event from reaching subsequent handlers.Bubble Phase: If the event wasn’t stopped in the capture phase, the framework then propagates the event upward, from the source component back to the application root. Again, any handler along this path can interact with the event and stop its propagation.
Default Phase: The default phase begins from the root node and executes any default event handling, unless
event.preventDefault()
was called in earlier phases. The root node will be set to either the application root or the component that invokedevent.stopPropagation()
.
Summary:
- Source Component: Fires the event.
- Capture Phase: Moves from application root down to the source.
- Bubble Phase: Propagates from the source back up to the root.
- Default Phase: Executes any default behavior unless blocked.
2. Component Events
Component events are similar to application events but are typically contained within the hierarchy of components. A component event can be handled by the component that fired it or any component higher in the containment hierarchy.
Phases of Component Event Propagation:
Event Fired: The component event is fired from a component instance.
Capture Phase: Just like application events, the capture phase begins at the application root and moves downward to the source component. Components along this path can handle the event. The event’s propagation can be stopped by calling
stopPropagation()
.Bubble Phase: If propagation wasn’t stopped during the capture phase, the event then bubbles up from the source component to the application root. Any handling event can stop further propagation during this phase.
Summary:
- Source Component: Fires the component event.
- Capture Phase: Executes from the application root to the source.
- Bubble Phase: Moves from the source back up to the root.
Key Takeaways:
Application Events are global and can be handled by any component that subscribes to the event, regardless of their containment hierarchy.
Component Events are more localized, and their propagation is restricted to components in the containment hierarchy.
Both types of events support capture and bubble phases, allowing developers to control how the event is handled and whether its propagation should be stopped at any point.
By leveraging these phases, developers can create more flexible, dynamic, and interactive Lightning apps.
Comments
Post a Comment