Inter-component events

At the time of writing, Salesforce has not exposed a means by which Lightning Web Components that are not nested within one another or even known by one another can communicate. For Lightning Aura components, these are called application events. While Salesforce works on closing this feature gap, the sample code in this chapter is utilizing a small utility library.

The pubsub component contains a utility library provided by Salesforce within their own code samples (at the time of writing) to demonstrate how inter-component communication can be achieved between loosely coupled components that may not be in the same parent-child hierarchy.

When users of the Race Overview page click on races in the Race Calendar, the Race Results component is automatically updated to show the applicable race results (you can try this out for yourself by clicking on Italy). These two components are not contained within one another; they are simply placed on the same Lightning page together. The publication and subscribe event pattern is ideal for this communication requirement.

You could create a Race Overview component that contains these two components and use the component event approach described in the previous section to communicate between them. However, if you do this, you reduce the flexibility of building with components and exposing them to your customers to reconfigure the layout of the page or use the components independently, as in the case of the utility bar discussed later in this chapter.

The raceCalendar.js controller imports the pubsub library fireEvent method and calls it when a race is clicked. The following lines of code highlight these two aspects. The raceCalendar.js file is as follows:

// Import the pubsub utility library
import { fireEvent } from 'c/pubsub';

// Code within the handleSelect method
fireEvent(
this.pageRef, 'raceSelected',
{ raceId: raceId, raceName: raceName });

The raceResults.js controller imports the pubsub library registerListener and unregisterAllListeners methods. These methods are called by the following methods to subscribe and unsubscribe to the event during component creation and deletion.

Here is raceResults.js:

connectedCallback() {
registerListener('raceSelected', this.handleRaceSelected, this);
}
disconnectedCallback() {
unregisterAllListeners(this);
}

The handleRaceSelected method updates two properties that are tracked by the framework. When changes occur in those properties, this triggers calls to Apex and updates to the component's UI. We will discuss communication with Apex later in this chapter:

handleRaceSelected(race) {
this.raceId = race.raceId;
this.raceName = race.raceName;
}
Check out the latest information in this Salesforce help topic for sending events between components that are not in the same parent-child hierarchy: https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.events_pubsub.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.173.227