As your component grows, you want to break it down to maintain granularity and encapsulation. This step walks you through creating a component with repeating data and whose attributes are passed to its parent component. You’ll also add a client-side controller action to load your data on component initialization.
Create expenseList.cmp
Create a sub-component expenseList that shows list of expenses.
- Click File > New > Aura Component.
- Enter expenseList in the New Aura Bundle window. This creates a new Aura component, expenseList.cmp.
- In expenseList.cmp, enter this code.
Code Highlights
- Instead of using
{!expense.amount}
, you’re now using{!v.expense.amount}
. This expression accesses the expense attribute and the amount value on it. The expense attribute is of typeExpense__c
. - For non-primitive types, use the format
myNamespace.myApexClass
. - Additionally,
href="{!'/' + v.expense.id}"
uses the expense id to set the link to the detail page of each expense record.
Add Styles
Click STYLE in the sidebar and enter these CSS rule sets.
Update form.cmp
In form.cmp,
- Add a List attribute that stores allExpenses.
<aura:attribute name="allExpenses" type="List" />
- Update the aura:iteration tag to use
{!v.allExpenses}
instead of.{!m.expenses}
Finally it should look like this
You’re now passing the whole expense object, represented by namespace:expenseList
, into the parent component using an attribute. The aura:attribute
tag represents a list of the expenses on an instance of the component. Next, create the event handler to load your model on component initialization.
Load Data Upon Initialization.
Aura automatically fires init
event when the component loads. We will add a event handler to listen to init
event and make call to SFDC and finally populate allExpenses
in the view.
Create Event Handler
In form.cmp, add an init handler immediately after the opening aura:component tag.
On initialization, this event handler runs the doInit action that you’re creating next
Create A Controller
Add the client-side controller action for this handler. In the sidebar, click CONTROLLER to create a new resource, formController.js
. Enter this code.
Save your changes and reload your browser. Your expense records are now displayed.
Test The App
Congratulations! Your app now loads and displays records from the model. Next, add client-side logic to process user input and save them in the model.