Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

suscribe messagechannel lwc

// lmsSubscriberWebComponent.js
import { LightningElement, wire } from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { reduceErrors } from 'c/ldsUtils';

// Import message service features required for subscribing and the message channel
import {
    subscribe,
    unsubscribe,
    APPLICATION_SCOPE,
    MessageContext
} from 'lightning/messageService';
import recordSelected from '@salesforce/messageChannel/Record_Selected__c';

import NAME_FIELD from '@salesforce/schema/Contact.Name';
import TITLE_FIELD from '@salesforce/schema/Contact.Title';
import PHONE_FIELD from '@salesforce/schema/Contact.Phone';
import EMAIL_FIELD from '@salesforce/schema/Contact.Email';
import PICTURE_FIELD from '@salesforce/schema/Contact.Picture__c';

const fields = [
    NAME_FIELD,
    TITLE_FIELD,
    PHONE_FIELD,
    EMAIL_FIELD,
    PICTURE_FIELD
];

export default class LmsSubscriberWebComponent extends LightningElement {
    subscription = null;
    recordId;

    Name;
    Title;
    Phone;
    Email;
    Picture__c;

    @wire(getRecord, { recordId: '$recordId', fields })
    wiredRecord({ error, data }) {
        if (error) {
            this.dispatchToast(error);
        } else if (data) {
            fields.forEach(
                (item) => (this[item.fieldApiName] = getFieldValue(data, item))
            );
        }
    }

    @wire(MessageContext)
    messageContext;

    // Encapsulate logic for Lightning message service subscribe and unsubsubscribe
    subscribeToMessageChannel() {
        if (!this.subscription) {
            this.subscription = subscribe(
                this.messageContext,
                recordSelected,
                (message) => this.handleMessage(message),
                { scope: APPLICATION_SCOPE }
            );
        }
    }

    unsubscribeToMessageChannel() {
        unsubscribe(this.subscription);
        this.subscription = null;
    }

    // Handler for message received by component
    handleMessage(message) {
        this.recordId = message.recordId;
    }

    // Standard lifecycle hooks used to subscribe and unsubsubscribe to the message channel
    connectedCallback() {
        this.subscribeToMessageChannel();
    }

    disconnectedCallback() {
        this.unsubscribeToMessageChannel();
    }

    // Helper
    dispatchToast(error) {
        this.dispatchEvent(
            new ShowToastEvent({
                title: 'Error loading contact',
                message: reduceErrors(error).join(', '),
                variant: 'error'
            })
        );
    }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: js organise string tab spaced 
Javascript :: onClick: share image on Facebook angular 9 
Javascript :: creating a react app from scratch 
Javascript :: correct code for the { "vars": "local" } 
Javascript :: using Canvas with tkinger draw arc 
Javascript :: on page navigate event javascript 
Javascript :: TypeError: Invalid schema configuration: `True` is not a valid type at path `id.required`. See https://bit.ly/mongoose-schematypes for a list of valid schema types.] 
Javascript :: Get the max value from array - divi modules 
Javascript :: Hexo - Execute Console Commands 
Javascript :: counter plus minus for quantity 
Javascript :: Plumsail change the size of the dialog window 
Javascript :: uncheck all other checkboxes when one is checked 
Javascript :: js how to shuffle array algoritm. The Fisher-Yates algorith 
Javascript :: Multiple Locale Support momentjs 
Javascript :: javascript Change color based on a keys value in each object of array 
Javascript :: javascript llop array 
Javascript :: angular create spec file for existing component 
Javascript :: angularjs New Entry Not reflacting in table after inserting New record in CRUD angular app 
Javascript :: Get value from each *ngFor ionic 4, ionic 5, ionic 6 
Javascript :: EXPO useEffect not called on navigating to same screen 
Javascript :: context Menus 
Javascript :: new Date() how can i ue 
Javascript :: Node.js with Express: Importing client-side javascript using script tags in Jade views 
Javascript :: Triggering An Event Programmatically With JavaScript 
Javascript :: Printer Errors 
Javascript :: Saving dependencies in your node package.json syntax 
Javascript :: Adding Proof of Work to blockchain 
Javascript :: underscore js 
Javascript :: kendo grid set page number 
Javascript :: prisma Return a relations count with include 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =