Sunday, August 19, 2018

Application Events : Way to Communicate Between Different Lightning Components

Application events are used to communicate between different lightning components. These are different from component events as these are not dependent on containment hierarchy.

Application events behaves similar to standard events. There is no need to register for application event by any component. Any component can fire application event where as in component events, components have to register first in order to fire that event.

Through this blog, I will explain different aspect of using application events:

I have created a lightning app which contains 2 separate components. User is entering some information and sending it to second component by clicking on button.



Below are different steps you need to follow in order to implement application events:

  • Create lightning event "SK_MessagerEvent" of type Application
       <aura:event type="APPLICATION" description="Event template" >    
            <aura:attribute name="msg" type="String" access="GLOBAL"/>                           
       </aura:event>

  • Use below syntax to fire event from lightning component:
        var appEvent = $A.get("e.c:SK_MessengerEvent");
        appEvent.setParams({"msg":component.get("v.ltngUserInput")}); 
        appEvent.fire();

  • Create handler in component which want to consume or catch the information passed using application events.
       <aura:handler action="{!c.handleNotification}" event="c:SK_MessengerEvent" />
  • Create function in controller.js which will get invoked whenever this application event will be fired.
        var sentMessage= event.getParam("msg"); 
        console.log('******sentMessage by app event:'+sentMessage);
        component.set("v.recMsg",sentMessage);

Below is complete code for your reference related to snapshot shown above:


Hope this help!!

More Blogs>>: 
COMPONENT EVENTS: Communicate Between Components in Containment Hierarchy    
INHERITANCE IN LIGHTNING    
FIRING EVENT FROM LIGHTNING COMPONENT AND PASSING IT TO VF PAGE    
CHANGES TO LIGHTNING DATA SERVICE IN SUMMER'17    
LIGHTNING DATA SERVICES    
PASSING LIGHTNING COMPONENT ATTRIBUTE VALUE FROM VF PAGE    
FIRE LIGHTNING EVENTS FROM VF PAGE    
DYNAMICALLY CREATING AND DESTROYING LIGHTNING COMPONENTS    
RAISING AND HANDLING CUSTOM EVENTS IN SALESFORCE LIGHTNING    
WHY TO USE DESIGN RESOURCE AND HOW TO ADD DYNAMIC OPTION TO DATASOURCE    
PASSING LIGHTNING COMPONENT ATTRIBUTE VALUE FROM VF PAGE    
PASSING INNER WRAPPER CLASS TO LIGHTNING COMPONENT    
LIGHTNING COMPONENT FOR RECORDTYPE SELECTION FOR ANY SOBJECT    
CUSTOM COMPONENT TO SHOW/HIDE SPINNER IMAGE    

No comments:

Post a Comment