Activity Forums Salesforce® Discussions What are the types of custom events in Salesforce Lightning condition?

  • Prachi

    Member
    August 1, 2018 at 7:30 am

    Hello shradha,

    Types of custom events:-

    Component-Level Events
    Component-level events are generated by a component and can be handled by the component itself, or its containing component or application. Component events are useful for situations where your component can be used in an application multiple times and you don’t want the different instances interfering with each other.

    Application-Level Events
    Application-level events are published by a component, and any component or application that has subscribed to the event has its handler invoked when the event is fired.

    Thanks.

  • shariq

    Member
    September 18, 2018 at 11:01 pm

    Hi,

    Here is the example -

    Componenet level event

    <aura:event type="COMPONENT" description="Fired when a Contact search is completed">
    <aura:attribute name="contacts" type="Contact[]" description="The result of the search, which could be an empty array"/>
    </aura:event>

    Application level event

    <!--c:aeEvent-->
    <aura:event type="APPLICATION">
    <aura:attribute name="message" type="String"/>
    </aura:event>

    Hope this helps.

  • Parul

    Member
    September 19, 2018 at 4:52 am

    All we know, that we have 2 types of Events

    Application Event: Application events follow 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.

    <!--c:aeEvent-->
    <aura:event type="APPLICATION">
    <aura:attribute name="message" type="String"/>
    </aura:event>
    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.

    <!--c:ceEvent-->
    <aura:event type="COMPONENT">
    <aura:attribute name="message" type="String"/>
    </aura:event>
    All components inside the Application will get notified when Application event fires. where as Component event only those component will notify which are in hierarchy.

    Thanks

  • Avnish Yadav

    Member
    September 30, 2018 at 12:37 pm

    Hello shradha,

    Types of custom events:-

    Component-Level Events
    Component-level events are generated by a component and can be handled by the component itself, or its containing component or application.

    <!–c:ceEvent–>
    <aura:event type=”COMPONENT”>
    <aura:attribute name=”message” type=”String”/>
    </aura:event>

    Application-Level Events
    Application-level events are published by a component, and any component or application that has subscribed to the event has its handler invoked when the event is fired.

    <!–c:aeEvent–>
    <aura:event type=”APPLICATION”>
    <aura:attribute name=”message” type=”String”/>
    </aura:event>

    Thanks.

  • Jade

    Member
    November 23, 2018 at 8:57 am

    Hello Shradha,

    Basically there are two types of Events as everyone answered -

    Let me tell you the basic difference and when to choose what -

    COMPONENT EVENT : It only works if one component is in Parent-child relationship with other component.

    In Other words-  To talk to a parent using the capture and bubbling mechanism, like with DOM events. Usually, one component is interested by the event, like an event aggregator.

    Component must reference events by name, much like an aura:id, and retrieve them from its component (hence component.get()) value provider:

    var evt = cmp.get("e.myEvent");

    You would then declare myEvent on the component as:

    <aura:registerEvent name="myEvent" type="namespace:eventName"/>

    Declaring events on a component allows other components to call controller methods. You had to declare a handler:

    <aura:handler name="myEvent" action="{!c.myEventHandler}"/>

    That allowed you to call myEventHandler from another component and respect the interface:

    component.get("e.myEvent").fire();

    For Example refer this Link : https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/events_component_example.htm

    APPLICATION EVENT : This will help you in communication between components which are not necessarily related.

    In other words :

    To broadcast to other components and not exclusively ancestors. Applications events can talk to many components that can be interested by the event. The broadcast can be boxed to an area of the DOM (everything below a DIV for example).

    This is obtained from the global (hence $A.get()) event value provider:

    var evt = $A.get("e.myNamespace:myEvent");

    For Example Refer this link : https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/events_application_example.htm

    Hope this will help.

    Thanks

     

    • This reply was modified 6 years, 1 month ago by  Jade.

Log In to reply.

Popular Salesforce Blogs