No Results
Search Tips:
- Please consider misspellings
- Try different search keywords
Newer Version Available
Step 6: Make the App Interactive With Events
Let’s start by creating the event and its handler before firing it and handling the event in the parent component.
- Click .
- Enter updateExpenseItem in the New Event window. This creates a new event, updateExpenseItem.evt.
-
In updateExpenseItem.evt, enter this code.
The attribute you’re defining in the event is passed from the firing component to the handlers.
Replace namespace with the name of your registered namespace.
1swfobject.registerObject("clippy.codeblock-0", "9");<aura:event type="APPLICATION"> 2 <aura:attribute name="expense" type="namespace.Expense__c"/> 3</aura:event>The framework has two types of events: component events and application events. An application event is used here, which when fired notifies its handlers. In this case, form.cmp is notified and handles the event.
Recall that expenseList.cmp contains the checkbox that’s wired up to a client-side controller action, denoted by change="{!c.update}". You’ll set up the update action next.
-
In the expenseList sidebar, click
CONTROLLER. This creates a new resource,
expenseListController.js. Enter this code.
Replace namespace with the name of your registered namespace.
1swfobject.registerObject("clippy.codeblock-1", "9");({ 2 update: function(component, evt, helper) { 3 var expense = component.get("v.expense"); 4 var updateEvent = $A.get("e.namespace:updateExpenseItem"); 5 updateEvent.setParams({ "expense": expense }).fire(); 6 } 7})When the checkbox is checked or unchecked, the update action runs, setting the reimbursed parameter value to true or false. The updateExpenseItem.evt event is fired with the updated expense object .
-
In the handler component, form.cmp, add this handler code
before the <aura:attribute> tags.
Replace namespace with the name of your registered namespace.
1swfobject.registerObject("clippy.codeblock-2", "9");<aura:handler event="namespace:updateExpenseItem" action="{!c.updateEvent}" />This event handler runs the updateEvent action when the application event you created is fired.
-
Wire up the updateEvent action to handle
the event. In formController.js, enter this code after the
createExpense controller action.
1swfobject.registerObject("clippy.codeblock-3", "9");updateEvent : function(component, event, helper) { 2 helper.upsertExpense(component, event.getParam("expense")); 3}This action calls a helper function and passes in event.getParam("expense"), which contains the expense object with its parameters and values in this format: { Name : "Lunch" , namespace__Client__c : "ABC Co." , namespace__Reimbursed__c : true , CreatedDate : "2014-08-12T20:53:09.000Z" , namespace__Amount__c : 20}.
Beyond the Basics
The framework fires several events during the rendering lifecycle, such as the init event you used in this tutorial. For example, you can also customize the app behavior during the waiting event when the client is waiting for a server response and when the doneWaiting event is fired to signal that the response has been received. This example shows how you can add text in the app during the waiting event, and remove it when the doneWaiting event is fired.
1<!-- form.cmp markup -->
2<aura:handler event="aura:waiting" action="{!c.waiting}"/>
3<aura:handler event="aura:doneWaiting" action="{!c.doneWaiting}"/>
4<aura:attribute name="wait" type="String"/>
5
6<div class="wait">
7 {!v.wait}
8</div>1/** formController.js **/
2waiting : function(component, event, helper) {
3 component.set("v.wait", "updating...");
4},
5doneWaiting : function(component, event, helper) {
6 component.set("v.wait", "");
7}The app displays this text when you click the Submit button to create a new record or when you click the checkbox on an expense item. For more information, see Events Fired During the Rendering Lifecycle.