Activity Forums Salesforce® Discussions What is the difference between component event and application event in Salesforce?

  • Aman

    Member
    July 11, 2017 at 3:50 pm

    Hello Shariq,

    Application Event:

    Application event follows a traditional publish-subscribe model. An application event is fired from an instance of a component. All components that provide a handler for the event are notified.

    Component Event:

    A component event is fired from an instance of a component. A component event can be handled by the component that fired the event or by a component in the containment hierarchy that receives the event.

  • Parul

    Member
    September 16, 2018 at 4:44 am

    Component Events 
    Components events can be handled by same component or component which is present in containment hierarchy (component that instantiates or contain component).
    Below is syntax for creating component event.

    <aura:event type="COMPONENT" description="Event template" >

    <aura:attribute name="msg" type="String" access="GLOBAL"/>

    </aura:event>

    Below is syntax for firing component events from javascript controller.

    var accidentEvent = component.getEvent("newCarAccident"); accidentEvent.setParams({"msg":"New Message!!!."});

    accidentEvent.fire();

    While handling component events, we need to specify name attribute in

    <aura:handler>

    <aura:handler action="{!c.handleNotification}" event="c:carAccidentComponentEvent" name="newCarAccident">

    Make sure that name attribute is same as that of name attribute while registering the event.

     

    Application Events

    This kind of events can be handled by any component which is listening to it (have handler defined for event). It is kind of publish-subscribe modal.
    Below is syntax of creating application event.

    <aura:event type="APPLICATION" description="Event template" >                                                     <aura:attribute name="msg" type="String" access="GLOBAL"/>

    </aura:event>

    Below is syntax to file application events from javascript controller.

    var appEvent = $A.get("e.c:carAccidentAppEvent");

    appEvent.setParams({"msg":"New Message!!!."});

    appEvent.fire();

    While handling application events, no need to specify name attribute in <aura:handler> 

    <aura:handler event="c:carAccidentAppEvent" action="{!c.handleNotification}"/>

     

    Thanks

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos