ExamGecko
Question list
Search
Search

List of questions

Search

Question 203 - Certified B2B Commerce Administrator discussion

Report
Export

A developer has created a custom Lightning web component for the Cart page that needs to react to changes to cart items from the standard cart component.

How should the developer implement the custom component so changes to cart items and quantities are reflected?

A.
Subscribe to events on the lightning_commerce_cartChanged channel using the Lightning Message Service.
Answers
A.
Subscribe to events on the lightning_commerce_cartChanged channel using the Lightning Message Service.
B.
Add a listener for the cartltemUpdate Lightning event.
Answers
B.
Add a listener for the cartltemUpdate Lightning event.
C.
Listen for events on the lightning_commerce_cartChanged channel with the Lightning Event ''Listener component.
Answers
C.
Listen for events on the lightning_commerce_cartChanged channel with the Lightning Event ''Listener component.
D.
Add an event listener for the cartchanged DOM (Document Object Model) event.
Answers
D.
Add an event listener for the cartchanged DOM (Document Object Model) event.
Suggested answer: A

Explanation:

The Lightning Message Service (LMS) is a feature that allows communication across different UI technologies, such as Lightning Web Components, Aura Components, and Visualforce pages. LMS uses message channels to publish and subscribe to messages.A message channel is a custom metadata type that defines the shape and scope of the messages1.The lightning_commerce_cartChanged channel is a standard message channel that is used by the B2B Commerce Cloud platform to notify components of changes to the cart items and quantities2. A developer can use the LMS API to subscribe to events on this channel and update the custom component accordingly.The developer needs to import the lightning/messageService module and the lightning_commerce_cartChanged channel in the JavaScript file of the custom component, and then use the subscribe method to register a callback function that handles the message payload3. For example:

// cartCustomComponent.js import { LightningElement, wire } from 'lwc'; import { subscribe, MessageContext } from 'lightning/messageService'; import CART_CHANGED_CHANNEL from '@salesforce/messageChannel/lightning_commerce_cartChanged__c';

export default class CartCustomComponent extends LightningElement { // Declare a message context @wire(MessageContext) messageContext;

// Declare a subscription variable subscription = null;

// Subscribe to the message channel in the connected callback connectedCallback() { this.subscribeToMessageChannel(); }

// Subscribe to the message channel using the LMS API subscribeToMessageChannel() { if (!this.subscription) { this.subscription = subscribe( this.messageContext, CART_CHANGED_CHANNEL, (message) => this.handleCartChange(message) ); } }

// Handle the message payload and update the component logic handleCartChange(message) { // Do something with the message payload, such as: // - Display the cart items and quantities // - Calculate the cart total // - Apply discounts or taxes // - etc. } }

asked 23/09/2024
Fernando Pereira dos Santos
39 questions
User
Your answer:
0 comments
Sorted by

Leave a comment first