Tuesday, July 17, 2018

Salesforce Lightning Events Part 1 - Component Events Introduction



A component event is fired from the instance of a component. It can be handled either by the component who has fired this event or any other component in the hierarchy that receives the component event and has a handler defined to handle that component event.

Propagation of Component Event

There are mainly two phases in component event propagation :-
  1. Bubble Phase
  2. Capture Phase

Bubble Phase

In this phase, when an event is fired, it'll traverse up in the hierarchy and will end at the application root. In this phase, any component in the hierarchy in which we have defined a handler for that event can handle that event or even stop it from propagating further towards application root.
In this phase, the event traverses from source component to top by traversing the components in hierarchy and will stop finally at the application root. The source component who fired the event, can easily handle that event without any restrictions as the propagation starts from itself.

Component Event Propagation in Bubble Phase

Capture Phase

In this phase, when an event is fired, it'll move directly to the application root and then from the application root it will move through the path that ends at the component who fired this event. While traversing this path, any other component in which we have defined a handler for that event can handle that event and even stop that event from propagating further in the hierarchy.
In this phase, the event traverses directly from source component to top and then move towards bottom by traversing the components in hierarchy back to source component. The event cannot be handled by the source component which fired the event if it's propagation is stopped by any other component in the hierarchy while traversing down from application root to source component.
Component Event Propagation in Capture Phase

By default, the component events traverse in Bubble Phase.

No comments:

Post a Comment