Activity › Forums › Salesforce® Discussions › What are the types of custom events in Salesforce Lightning condition?
-
What are the types of custom events in Salesforce Lightning condition?
Posted by shradha jain on July 23, 2018 at 10:04 AMWhat are the types of custom events in Salesforce Lightning condition? Explain all the types in detail.
Jade replied 7 years, 5 months ago 6 Members · 5 Replies -
5 Replies
-
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.
- [adinserter block='9']
-
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
<aura:event type=”APPLICATION”>
<aura:attribute name=”message” type=”String”/>
</aura:event>Hope this helps.
-
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.
<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.
<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
-
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.
-
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 7 years, 5 months ago by
Jade.
-
This reply was modified 7 years, 5 months ago by
Log In to reply.