Newer Version Available

This content describes an older version of this product. View Latest

Java Models

Use a Java model to read a component's data from a dynamic source, such as a database. The component generates an appropriate user interface from the model's data.

The value provider for a model is denoted by m. For example, the label in this button component is retrieved from the model of the component containing the <ui:button> tag. The value for the label is evaluated when the component renders.

1swfobject.registerObject("clippy.codeblock-0", "9");<ui:button label="{!m.myLabel}"/>

On the server side, Aura's model is more of a model initializer compared to the usage of models in other MVC frameworks. The model is instantiated when the component is first requested. Perform any necessary operations to gather state, such as making database queries or external API callouts, in the model's constructor.

When the component is serialized to the client, the @AuraEnabled getters are executed, and their results are serialized as name-value pairs. This serialized map becomes the basis for the initial state of the model on the client.

You can't create a new component dynamically in a model class using Aura.getInstanceService().getInstance().

Note

Wiring Up the Model

The aura:component tag contains a model system attribute that wires it to the Java model. For example:

1swfobject.registerObject("clippy.codeblock-1", "9");<aura:component model="java://org.auraframework.demo.notes.models.TrivialModel">

Accessing the Model in Markup

Let's look at simple usage of a model in the markup of a component.

1swfobject.registerObject("clippy.codeblock-2", "9");<aura:component model="java://org.auraframework.demo.notes.models.TrivialModel">
2    <aura:attribute name="name" type="String" required="true" default="Michelle" />
3
4    <!-- Use the "m." prefix to access any fields that are annotated with
5    @AuraEnabled in the model class -->
6    <h1>Title : {!m.title}</h1>
7
8    <!-- Use v.name to directly access the component's name attribute.
9        Remember that you use v to access the component's attribute values -->
10    <h2>Name : {!v.name}</h2>
11</aura:component>

The {!m.title} expression returns the result of the getTitle() getter method in the component's model class. The getTitle() method must be prefixed with the @AuraEnabled annotation.

Java Model class

This model is simple as it doesn’t read in data from a persistent data store but it demonstrates some basics, including accessing a component's attribute in the model.

1swfobject.registerObject("clippy.codeblock-3", "9");package org.auraframework.demo.models;
2
3import org.auraframework.instance.BaseComponent;
4import org.auraframework.system.Annotations.AuraEnabled;
5import org.auraframework.system.Annotations.Model;
6import org.auraframework.throwable.quickfix.QuickFixException;
7
8@Model
9public class TrivialModel throws QuickFixException {
10
11    private String title;
12
13    // The constructor is called during the construction of each instance of the model
14    // The constructor must be public
15    public TrivialModel() {
16       // This retrieves the component for this model as a Java object
17       BaseComponent cmp =
18      Aura.getContextService().getCurrentContext().getCurrentComponent();
19
20       // Retrieve the name attribute of the component
21       String name = (String)cmp.get("v.name");
22
23       /* Do any queries or data generation in the constructor of your model.
24        * In this sample, we have a trivial initialization for the title field.
25        * A real-world scenario would read the data from a persistent data store. */
26       title = "Welcome to " + name;
27    }
28
29    // Use @AuraEnabled to enable client- and server-side access to the title field
30    @AuraEnabled
31    public String getTitle() {
32       return title;
33    }
34}

Java Annotations

These annotations are available in Java models.

Annotation Description
@Model Denotes that a Java class is a model.
@AuraEnabled Enables client- and server-side access to a getter method. This means that you only expose data that you have explicitly annotated and avoids accidentally exposing fields. Other fields are not available.

Learn More

For a more in-depth example of a model that initializes its data from a database, see the NoteListModel class and the noteList.cmp component in the Aura Note sample app.