Load the Expense Data
Models load initial data and are created in an Apex class.
Expense Class
Create an Expense object and define a list variable. This list is loaded in your app using a getter method.
- Click File > New > Apex Class.
- Enter Expense in the New Class window. This creates a new Apex class,
Expense.apxc
. - Enter this code to create an Expense object.
Replace namespace with the name of your registered namespace.
The Expense object contains variables that correspond to the custom fields you created. get
and set
are Apex properties that make the variables readable and writeable. @AuraEnabled exposes those properties to your Aura app.
ExpenseModel Class
- Create the
expense
model. Click File > New > Apex Class and enter ExpenseModel in the New Class window. This creates a new Apex class,ExpenseModel.apxc
. - Enter this code.
The loadExpenses() method runs a SOQL query to return all expense records. The for loop stores your expense records in a expenseSobjects variable. getExpenses() returns the list of records to the component using the m.expenses
syntax.
Update form.cmp
Go back to form.cmp component. Then update the aura:component tag to include the model attribute after the <component>
tag (i.e. 2nd line). <aura:component model="namespace.ExpenseModel">
Test The App
Save your changes and reload your browser. You should see the expense records. The counters aren’t working at this point as you’ll be adding the programmatic logic later.
Summary
In this step, you created a model to load initial expense data. The model contains a SOQL query. getExpenses() returns the list of expense records. By default, Aura doesn’t call any getters. To access a method, annotate the method with @AuraEnabled
, which exposes only the data in that method. Models are instantiated when the component is first requested.