The aeNotifier.cmp notifier component uses aura:registerEvent to declare that it may fire the application event. The name attribute is required but not used for application events. The name attribute is only relevant for component events.
The button in the component contains a onclick browser event that is wired to the fireApplicationEvent action in the client-side controller. Clicking this button invokes the action.
1<!--c:aeNotifier-->2<aura:component>3 <aura:registerEvent name="appEvent" type="c:aeEvent"/>45 <h1>Simple Application Event Sample</h1>6 <p><lightning:button7 label="Click here to fire an application event"8 onclick="{!c.fireApplicationEvent}" />9 </p>10</aura:component>
The client-side controller gets an instance of the event by calling $A.get("e.c:aeEvent"). The controller sets the message attribute of the event and fires the event.
1/* aeNotifierController.js */2{3 fireApplicationEvent : function(cmp, event){4 // Get the application event by using the5 // e.<namespace>.<event> syntax6 var appEvent = $A.get("e.c:aeEvent");7 appEvent.setParams({8 "message" : "An application event fired me. " +9 "It all happened so fast. Now, I'm everywhere!"});10 appEvent.fire();11}12}
Handler Component
The aeHandler.cmp handler component uses the <aura:handler> tag to register that it handles the application event.
The handler for an application event won’t work if you set the name attribute in <aura:handler>. Use the name attribute only when you’re handling component events.
Note
When the event is fired, the handleApplicationEvent action in the client-side controller of the handler component is invoked.
The controller retrieves the data sent in the event and uses it to update the messageFromEvent attribute in the handler component.
1/* aeHandlerController.js */2{3 handleApplicationEvent : function(cmp, event){4 var message = event.getParam("message");56 // set the handler attributes based on event data7 cmp.set("v.messageFromEvent", message);8 var numEventsHandled = parseInt(cmp.get("v.numEvents")) + 1;9 cmp.set("v.numEvents", numEventsHandled);10}11}
Container Component
The aeContainer.cmp container component contains the notifier and handler components. This is different from the component event example where the handler contains the notifier component.