The aeEvent.evt application event has one attribute. We’ll use this attribute to pass some data in the event when it’s fired.
<!--c:aeEvent--> <aura:event type="APPLICATION"> <aura:attribute name="message" type="String"/> </aura:event>
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.
<!--c:aeNotifier--> <aura:component> <aura:registerEvent name="appEvent" type="c:aeEvent"/> <h1>Simple Application Event Sample</h1> <p><lightning:button label="Click here to fire an application event" onclick="{!c.fireApplicationEvent}" /> </p> </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.
/* aeNotifierController.js */ { fireApplicationEvent : function(cmp, event) { // Get the application event by using the // e.<namespace>.<event> syntax var appEvent = $A.get("e.c:aeEvent"); appEvent.setParams({ "message" : "An application event fired me. " + "It all happened so fast. Now, I'm everywhere!" }); appEvent.fire(); } }
The aeHandler.cmp handler component uses the <aura:handler> tag to register that it handles the application event.
When the event is fired, the handleApplicationEvent action in the client-side controller of the handler component is invoked.
<!--c:aeHandler--> <aura:component> <aura:attribute name="messageFromEvent" type="String"/> <aura:attribute name="numEvents" type="Integer" default="0"/> <aura:handler event="c:aeEvent" action="{!c.handleApplicationEvent}"/> <p>{!v.messageFromEvent}</p> <p>Number of events: {!v.numEvents}</p> </aura:component>
The controller retrieves the data sent in the event and uses it to update the messageFromEvent attribute in the handler component.
/* aeHandlerController.js */ { handleApplicationEvent : function(cmp, event) { var message = event.getParam("message"); // set the handler attributes based on event data cmp.set("v.messageFromEvent", message); var numEventsHandled = parseInt(cmp.get("v.numEvents")) + 1; cmp.set("v.numEvents", numEventsHandled); } }
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.
<!--c:aeContainer--> <aura:component> <c:aeNotifier/> <c:aeHandler/> </aura:component>
You can test this code by adding <c:aeContainer> to a sample aeWrapper.app application and navigating to the application.
https://<myDomain>.lightning.force.com/c/aeWrapper.app, where <myDomain> is the name of your custom Salesforce domain.
If you want to access data on the server, you could extend this example to call a server-side controller from the handler’s client-side controller.