In this exercise you will use the ArrayCollection object
that you created in Exercise 4.7 to
populate a Spark List control. You will also use the labelFuction property
of the control to format the data that is displayed in it.
In order to allow the data to be bound to the List control, you will also need to make the value object class bindable.
Lastly, you will create a two-way binding to populate a TextInput control. As you change the value in the TextInput control, you
will update the data in the ArrayCollection object, which will immediately
update the List control.

Figure 1. Review your task in this exercise.
In this exercise, you will learn how to:
In order to complete this tutorial, you need the following software and files:
In this section, you will make the Employee ActionScript
class bindable.
Make the Employee class definition bindable by adding a Bindable metadata tag:
package valueObjects
{
[Bindable]
public class Employee
{
public var firstName:String;
public var lastName:String;
...
In this section, you will use two Flex controls to display data from the data model.
UI components comment. Below the Label control, add a List control:
<s:Label text="Employee Directory"
height="24"
color="#028198"
fontSize="24"
fontWeight="bold"/>
<s:List/>
To the List control, assign employeeList as the id property, bind the dataProvider property to the employees ArrayCollection variable, the labelField to firstName and assign the height to 100:
<s:List id="employeeList"
dataProvider="{employees}"
labelField="firstName"
height="100"/>
Script block. Make the employees variable bindable by adding the Bindable metadata tag:
[Bindable] private var employees:ArrayCollection = new ArrayCollection;
You should see the List control populated with the first name of the employees.
In this section, you will update the List control to display
both the employee's first and last names.
Locate the List control and delete the labelField property and replace it with the labelFunction property with a value of employeeLabelFunction.
<s:List id="employeeList"
dataProvider="{employees}"
labelFunction="employeeLabelFunction"
height="100"/>
Below the last function in the Script block, create a private function named employeeLabelFunction that returns a value data typed to the String class:
...
private function employeeLabelFunction():String
{
}
]]>
</fx:Script>
Make the employeeLabelFunction() require one parameter named item that is data typed to the Object class:
private function employeeLabelFunction(item:Object):String
{
}
Within the function, declare a variable
named fullname and data type it to the String class:
private function employeeLabelFunction(item:Object):String
{
var fullname:String;
}
Assign the fullname variable to
the item.firstName and item.lastName values combined with a space between them:
private function employeeLabelFunction(item:Object):String
{
var fullname:String = item.firstName + " " + item.lastName;
}
Use the return operation to
return to the fullname variable to the
application:
private function employeeLabelFunction(item:Object):String
{
var fullname:String = item.firstName + " " + item.lastName;
return fullname;
}
Run the Employee Directory application.
You should see the List control populated with the first and last names of the employees (see Figure 2).

Figure 2. Run the application to see both the employee's first and last names displayed.
List control.Below the List control, create a TextInput control:
<s:List id="employeeList"
dataProvider="{employees}"
labelFunction="employeeLabelFunction"
height="100"/>
<s:TextInput/>
Assign the TextInput control an id of empNameInput,
a y value of 150 and bind the text value to the employee's first name selected in the List control.
Use the getItemAt() method of the ArrayCollection class
to retrieve the firstName value of the List controls selectedIndex index. Remember the List control is populated using the employees
ArrayCollection variable.
<s:TextInput id="empNameInput" y="150"
text="{employees.getItemAt(employeeList.selectedIndex).firstName}"/>
Save the file and run the application.
You should see the TextInput control is
populated (see Figure 3).

Figure 3. Click the first employee to see the TextInput control populated with the first employee's first name.
Click the first employee in the List control.
Within the TextInput control, type
the name Frank.
You should see the List control remains
unchanged from the text entered into the TextInput control (see
Figure 4).

Figure 4. Change the name in the TextInput control to Frank.
In this section, you will use a two-binding to update the
employee first name in the source employees ArrayCollection instance data
using the TextInput control.
TextInput control. Within the control's text property value, add the @ character at the beginning of the assignment
to create a two-way binding:
<s:TextInput id="empNameInput" y="150"
text="@{employees.getItemAt(employeeList.selectedIndex).firstName}"/>
Within the TextInput control, change the name to Frank.
You should see the first employee's first name value changes
to reflect the value in the TextInput control (see Figure 5).

Figure 5. Change the first name of the first employee.
In this exercise, you learned how to make an ActionScript class bindable, display the data model in another view, and use a two-way binding.
Bindable metadata tag..labelFunction property.Trilemetry, Inc is a development and education organization that implements a human-centered design approach to the creation of software and content. Their Adobe portfolio includes the Adobe ColdFusion Getting Started Experience, the Adobe Flex Getting Started Experience, the Flex in a Week video series, the official Adobe instructor-led training course Flex 3: Extending and Styling Components and more. They also create and support many Web applications from interactive Flash sites and corporate web sites to mission-critical business applications.