22 March 2010
In order to successfully complete this tutorial, be sure to set up and test your environment as outlined in Getting started with ColdFusion and Flash Builder 4.
You can skip the steps above if you have already successfully completed the "Getting started with ColdFusion and Flash Builder 4" tutorial or any other tutorial in this series.
This tutorial is intended for:
All
Adobe Flash Builder 4 has a form wizard that lets you quickly build a master/detail interface from server-side data with very little coding. You define the UI control that will display the master list of users to edit, and then you generate a form to display and edit the detail information.
In this tutorial, you will populate a ComboBox control with a data list of employees and customize the display. Next, you will generate a form that presents editable information about a selected employee. Lastly, you implement a button to update the employee's record in the database.
As mentioned in the introduction, this tutorial uses the same development environment as the Getting started with ColdFusion and Flash Builder 4 tutorial and the other tutorials in this series.

If you do not see the data type, follow the steps below to assign your own data type name to the returned data that is retrieved in the getAllData() service call to the CFC method.

Each record from the query has been placed in an ActionScript object with each column of data (FIRSTNAME, LASTNAME, EMAIL, and so on) set in a name/value pair. Each record is indexed starting at zero.
Note: Unlike ColdFusion, ActionScript is a zero-indexed language.
Rather than generically referring to the returned data as an object, the wizard lets you to give the data a return type name. You will do this next.



You will see the EmployeeSalesData data type listed in the Data Types category of the Data/Services view (see Figure 6). Note the properties of the data type are the query columns returned from the CFC method.

In this section, you will learn how to populate a ComboBox control with employee sales data to create a master control.
Note: In practice you will usually have only one main application file in a Flash Builder project. You will create a new main application file for each tutorial you follow in this series to avoid creating additional Flash Builder projects.
You will use the ComboBox control for the master user interface element. You could also use the List control or DataGrid control or any other list-based control.

You will now use the getAllData() CFC function to display the last name of the employee in the ComboBox control.
getAllData() function of the F4CF_Getting_Started_Service onto the ComboBox control (see Figure 8).

Note: You should see the ComboBox control now includes an icon indicating it is bound to data (see Figure 10).



By default the Bind To Data dialog box only lets you bind one field from the database to the ComboBox display. In the case of an employee name, this does not work well because there may be more than one employee with the same first name or last name.
In this section you learn how to bypass this default behavior by implementing an ActionScript function to display both the first name and last name for each employee.
Note: The code or control that you have selected in one mode will be selected when you switch to the other mode.
Note that the ComboBox code defines many properties including the x and y position for the control. The creationComplete property is actually an event that directs the application to retrieve the data from the getAllData() operation that you mapped to the CFC from the Data/Services view earlier. The dataProvider property takes the result of that service call and binds it to the ComboBox control. This property binds and stores all of the columns of data from the database to the ComboBox control, even though only one field is displayed. The curly braces around the value of the dataProvider property tell the application that this value is dynamic.
The LabelField property declares the variable that is displayed in the ComboBox control. When you selected LASTNAME from the Bind To Data dialog box, the wizard added the LASTNAME field to the LabelField property to tell the ComboBox control to display that value.
In the following steps, you will use the LabelFunction property to call a custom function that you write to combine the LASTNAME and FIRSTNAME variables in the ComboBox control display.
<fx:Script> tag block., create a private function named displayFullName using the following syntax:<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
import mx.controls.Alert;
protected function comboBox_creationCompleteHandler(event:FlexEvent):void
{
getAllDataResult.token = f4CF_Getting_Started_Service.getAllData();
}
private function displayFullName()
{
}
]]>
</fx:Script>
private function displayFullName():String
{
}
For each employee record that the ComboBox control displays, it will run this function that you just created. Remember that the dataProvider contains all of the data for each employee and that the ComboBox control doesn't know what you want to display so it needs to pass all of the data to you so that you can pick and choose what you want.
The application passes the data to the function in a complex object with name/value pairs. You can name the object anything you want, but the convention is to name it item. You will also data type it as a generic ActionScript Object instance.
displayFullName() function, add the item argument and data type it to the Object class:private function displayFullName(item:Object):String
Now you can combine the first and last name of each employee listed in the database using a simple string concatenation. Remember that the two columns in the database are FIRSTNAME and LASTNAME and are referenced through the item object that is passed to the function.
return keyword to return the FIRSTNAME and LASTNAME fields to the ComboBox display; make sure to add a space between the fields so they are displayed conventionally. private function displayFullName(item:Object):String
{
return item.FIRSTNAME + " " + item.LASTNAME;
}
labelField="LASTNAME" property.labelFunction property to the ComboBox control tag, setting its value equal to displayFullName.
Your ComboBox control MXML code should look as follows:
<mx:ComboBox x="71" y="56" editable="true" id="comboBox"
creationComplete="comboBox_creationCompleteHandler(event)"
dataProvider="{getAllDataResult.lastResult}" labelFunction="displayFullName"/>
Do not use the parentheses for the function in the labelFunction property. You are just providing the name of the function to the ComboBox control, not directly invoking the method.
The employees' full names are now displayed (see Figure 13).

In this section, you will use the Flash Builder Form Generation wizard to create a detail form that uses the master control (in this case the ComboBox control) as its data provider.



You should see the form populate with the employee sales data for the selected employee (see Figure 17).

In this section you will add a Button control to the interface that will allow users to update the employee information on the server.
Click the refresh button to the right of the Design mode button if the Button does not appear when you drop it.
If there is not a Submit button which was automatically generated, then drag and drop a Button control from the Components view into the Design area below the Details form.

updateItem() function of the F4CF_Getting_Started_Service data service.updateItem() function onto the Update button (see Figure 19). This associates the updateItem() service operation with the Update button.
If the view does not automatically switch, click the Source button to switch to Source mode.
Flash Builder generates a custom ActionScript click handler function when you drop the updateItem() function onto the Button control.
Flash Builder should have selected the word item between the parentheses of the updateItem() service call:

Note: There are many types of events in ActionScript. The MouseEvent data type for the event argument declares that this code is written for user mouse interactions. For more information about ActionScript events, see the Handling user events and Introducing the event object videos in the Flex in a Week training series.
The fact that the word item is used here is only coincidental to the object name in the ComboBox control. Remember that the CFC defined the name of the expected argument for the updateItem() method as item.
What you want to pass to the updateItem() operation is the service object that contains all of the data passed about the individual employee. This information is stored in the EmployeeSalesData data type that you named earlier.
employeeSalesData:
This will pass the employeeSalesData object, with all the changes made from the form, back to the server for updating in the database.

You should see that the employee's information contains the new values you assigned.
In this tutorial you learned how to use Flash Builder to create a master/detail interface with very little coding. You also learned how to customize the data displayed in a ComboBox control. You can learn more about returned data and data types by exploring the Generating forms with Adobe Flash Builder 4 tutorial.