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.
1<aura:event type="COMPONENT"> 2 <!-- If you’re using a namespace, use myNamespace.Expense__c instead. --> 3 <aura:attribute name="expense" type="Expense__c"/> 4</aura:event>The framework has two types of events: component events and application events.We’ll use a component event. Recall that expenseList.cmp contains the Reimbursed? checkbox.
-
Update expenseList.cmp to register that it fires the
event. Add this tag after the <aura:attribute> tag.
1<aura:registerEvent name="updateExpense" type="c:updateExpenseItem"/>The Reimbursed? checkbox is 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.
1({ 2 update: function(component, evt, helper) { 3 var expense = component.get("v.expense"); 4 // Note that updateExpense matches the name attribute in <aura:registerEvent> 5 var updateEvent = component.getEvent("updateExpense"); 6 updateEvent.setParams({ "expense": expense }).fire(); 7 } 8})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.
1<aura:handler name="updateExpense" event="c:updateExpenseItem" action="{!c.updateEvent}" />This event handler runs the updateEvent action when the component event you created is fired. The <aura:handler> tag uses the same value of the name attribute, updateExpense, from the <aura:registerEvent> tag in c:expenseList
-
Wire up the updateEvent action to handle
the event. In formController.js, enter this code after the
createExpense controller action.
1updateEvent : 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" , Client__c : "ABC Co." , Reimbursed__c : true , CreatedDate : "2014-08-12T20:53:09.000Z" , Amount__c : 20}.
- Save your changes and reload your browser.
-
Click the Reimbursed? checkbox for one of the records.
Note that the background color for the record changes. When you change the reimbursed status on the view, the update event is fired, handled by the parent component, which then updates the expense record by running the server-side controller action saveExpense.