Activity › Forums › Salesforce® Discussions › How to register, fire and handle a component and application event in Salesforce?
-
How to register, fire and handle a component and application event in Salesforce?
Posted by ABHISHEK SHAHI on September 24, 2018 at 3:21 AMHow to register, fire and handle a component and application event in Salesforce?
Avnish Yadav replied 7 years, 8 months ago 3 Members · 2 Replies -
2 Replies
-
We register an event by using the following code
<aura:registerEvent name=”sampleComponentEvent” type=”c:compEvent”/>
We fire event as shown below:
var compEvent = cmp.getEvent(“sampleComponentEvent”); compEvent.fire();
Handle component event as below:
<aura:handler name=”sampleComponentEvent” event=”ns:eventName” action=”{!c.handleComponentEvent}” phase=”capture” />Handle Application event as below:
<aura:handler event=”c:appEvent” action=”{!c.handleApplicationEvent}”/> - [adinserter block='9']
-
Fire Application Events
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.Register an Event
A component registers that it may fire an application event by using <aura:registerEvent> in its markup. The nameattribute is required but not used for application events. The name attribute is only relevant for component events. This example uses name=”appEvent” but the value isn’t used anywhere.<aura:registerEvent name="appEvent" type="c:appEvent"/>
Fire an Event
Use $A.get(“e.myNamespace:myAppEvent”) in JavaScript to get an instance of the myAppEvent event in the myNamespacenamespace.The syntax to get an instance of an application event is different than the syntax to get a component event, which is cmp.getEvent(“evtName”).
Use fire() to fire the event.var appEvent = $A.get("e.c:appEvent"); // Optional: set some data for the event (also known as event shape) // A parameter’s name must match the name attribute // of one of the event’s <aura:attribute> tags //appEvent.setParams({ "myParam" : myValue }); appEvent.fire();Thanks.
Log In to reply.