22 March 2010
In order to complete this tutorial successfully, be sure to set up and test your environment as outlined in Getting started with ColdFusion and Flash Builder 4.
All
Adobe Flash Builder 4 , formerly known as Flex Builder, has a very simple interface for binding server-side data to Flex framework UI controls. Using Design mode, you can drag and drop components to create a layout and bind the data. In a matter of minutes, you will have a dynamically populated application without writing any code.
In this tutorial, you will learn how to use DataGrid and PieChart controls and bind them to the data returned from a ColdFusion Component (CFC) function. You will also learn how to synchronize the data in the two controls.
This tutorial is intended for:
As mentioned in the introduction, this tutorial uses the same development environment as Getting started with ColdFusion and Flash Builder 4 and the other tutorials in this series. In this section, you will name a second data type for use with a different service call to a CFC function:
getEmployeeDataByRegion() function of the F4CF_Getting_Started_Service service and select Configure Return Type (see Figure 2).
Each record from the query will be 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.
Note: This data type differs from the EmployeeSalesData data type defined in the Getting started with ColdFusion and Flash Builder 4 tutorial because this one requires that you supply an argument, named region, to limit the data being returned to employees in a specific region. It also returns fewer columns of data from the database.
You must enter an argument value of Central to return the query results. This is just a mechanism to demonstrate that the query is working correctly so that you can define the return type name for it. In later steps, you will change the code to actually pass the argument value in the application.
You will see the EmployeeSalesDataByRegion data type listed in the Data Types category of the Data/Services view as shown in Figure 6. Note that the properties of the data type are the query columns returned from the CFC method.
The data returned from the CFC contains employee information associated with a region: Northwest, Southwest, Central, Northeast, and Southeast. See Deploying a Flex application with ColdFusion URL variables to learn more about passing dynamic data to an application. In this tutorial, you will hard-code the region value.
In this section, you will bind EmployeeDataByRegion data to a Flex framework DataGrid control. You will not need to write any code to accomplish this, instead you will use a built-in Flash Builder wizard to generate all the code.
You will use the drag-and-drop capabilities in the Flash Builder Design mode to add your DataGrid component:
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 projects.
You will now drag and drop the function from the Data/Services view onto your DataGrid control to bind the returned data:
getEmployeeDataByRegion() function in the Data/Services view.You will be switched to Source mode with the region variable highlighted in the code for the creation of the DataGrid control (see Figure 8).
'Central':protected function dataGrid_creationCompleteHandler(event:FlexEvent):void
{
getEmployeeDataByRegionResult.token = f4CF_Getting_Started_Service.getEmployeeDataByRegion('Central');
}
Note: As mentioned earlier, you are setting the getEmployeeDataByRegion() function's attribute, region, to a hard-coded value. See Deploying a Flex application with ColdFusion URL variables to learn more about passing dynamic data to an application.
You should see the DataGrid control column titles reflect the bound data (see Figure 9).
You should see the DataGrid control populated with data from the ColdFusion data source, as shown in Figure 12.
In this section, you will bind data to a Flex framework PieChart control. As with the DataGrid control, you will not need to write any code to accomplish this.
Again, you will use the drag-and-drop capabilities in the Flash Builder Design mode to lay out the PieChart control in the application:
You should see the Create Chart dialog box shown in Figure 14. Note that the Series elements area is pre-populated with an element named Series 1.
Not all data that is attached to a UI control needs to be displayed. The series property for a PieChart control allows you to define the exact data fields that you want displayed in the chart. You can plot more than one series of data fields on a chart, but you will only use one in this example.
Note: You may need to click the Refresh button to the right of the Design mode button to display the chart. You can also double-click the Editor tab that contains the MXML filename to expand and collapse the Design area. If the legend shows up in the upper left corner of the Design mode, drag and drop it next to the PieChart control.
In this section you will bind data to the PieChart control:
getEmployeeDataByRegion() function that you bound to the DataGrid control onto the PieChart control. Note that by dragging a new instance of the getEmployeeDataByRegion() function call to the PieChart control, you are actually calling that function twice in this application: once for the DataGrid control and once for the PieChart control.
You should see the Bind To Data dialog box (see Figure 14). Note the table under the Configure chart heading with the three columns: Series, Field, and Name.
You will be switched to Source mode with the region attribute highlighted in the getEmployeeDataByRegion() function call of the PieChart control (see Figure 15).
'Central'.Note: Remember that we are hard-coding this value for the purposes of the tutorial.
protected function YTDPieChart_creationCompleteHandler(event:FlexEvent):void
{
getEmployeeDataByRegionResult2.token = f4CF_Getting_Started_Service.getEmployeeDataByRegion('Central');
}
Note: If you do not have a licensed copy of Flex Builder 4 on your system you will see a watermark displayed over the PieChart control.
Note that there is no change in the PieChart control data. Remember that you made two separate calls to the service and retrieved two separate instances of returned data.
In this section, you will bind the PieChart control to the same data that is bound to the DataGrid control:
You should see the Bind Operation dialog box with the message shown in Figure 18.
Note that getEmployeeDataByRegionResult is the result data from the service call bound to the DataGrid control, and that getEmployeeDataByRegionResult2 is the result data from the service call bound to the PieChart control.
In this tutorial you learned how to use Flash Builder to easily bind server data to your Flex framework components and how doing so can make the components interactive. To learn more about service calls, returned data, and data types, refer to the Generating forms tutorial.
To learn more about how Flex and ColdFusion work together, refer to the following tutorials: