1 June 2009
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 Data Management wizard that makes it easy for you to map ColdFusion server-side create, retrieve, update, and delete (CRUD) business logic to data service calls and then apply them to UI controls.
In this tutorial, you will create a data service for the CFC and then use the Flash Builder Data Management wizard to map CRUD operations between the application and the server by creating ActionScript event handlers for the UI controls.
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:
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 this returned data as an object, this 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 5). Note that the properties of the data type are the query columns returned from the CFC method.
In this section, you will use the Data Management wizard to map CRUD functionality to the appropriate service call for adding, updating, and deleting records in a database.
You must choose the SALESTARGETID as the unique identifier for the EmployeeSalesData data type, because it is the primary key of the database.
In the Map Data Management Operations dialog box, you must select the appropriate service call for each field.
createItem() service operation from the popup menu.getItem() for the Get Item operation.updateItem() for the Update Item operation.deleteItem() for the Delete Item operation.Figure 7 shows what your Map Data Management Operations dialog box should look like at this point.
You learned earlier that the arguments for each service were introspected from the CFC by the Flash Builder Data Service wizard. For the add and update operations, you will pass a complex object, in this case the EmployeeSalesData data type, to the CFC. The getItem() and deleteItem() operations only need to pass the the primary key, SALESTARGETID, for processing.
You should see that the EmployeeSalesData data type now has the data management icon (two green arrows in opposite directions) applied to it (see Figure 8). Also, note that the SALESTARGETID data object now has a key icon next to it, indicating that it is the primary key of the managed data.
In this section, you will populate a DataGrid control with employee sales information.
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.
getAllData() function onto the DataGrid control (see Figure 11). You should see the DataGrid control populate the column titles with the columns from the database.
You will see the DataGrid control populated with all of the data from the database (see Figure 14). Note that you can edit all of the cells in the DataGrid control except the primary key values.
In this section you will add Button controls to the display that the user will click to add, delete, or save the employee sales data. Next, you will have Flash Builder generate the skeleton code for the Button control click handlers. Lastly, you will write some simple ActionScript logic to send the data back to the server.
In the following steps, you will add Button controls to the application display that users will use to trigger add, delete, and save functionality.
In the following steps, you will create a click handler for the Create Button control that will add records to the DataGrid control.
Flash Builder 4 will generate a custom ActionScript click handler function and switch you to Source mode so that you can add the business logic to it.
Note: ActionScript supports many types of events. 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 video training series.
//TODO Auto-generated method stub comment with a new ActionScript variable that you create using the var keyword. Name it employee and give it a data type of EmployeeSalesData. Set the variable equal to a new instance of the EmployeeSalesData().protected function
createButton_clickHandler(event:MouseEvent):void
{
var employee:EmployeeSalesData = new
EmployeeSalesData();
}
This code creates a new instance of the EmployeeSalesData data type that you defined earlier. Remember that each record that was received in the getAllData() service operation is an instance of the EmployeeSalesData, which contains the values for the SALESTARGETID, FIRSTNAME, LASTNAME, and all the other properties from the database. If you cut-and-pasted the code, Flash Builder will not have added the import statement. Manually add it now above the other import statements.
new EmployeeSalesData() statement that Flash Builder automatically added the following import statement to the beginning of the Script block that will allow you to use this class in ActionScript code.import valueObjects.EmployeeSalesData;
SALESTARGETID, FIRSTNAME, LASTNAME, PHONE, EMAIL, REGION, EMPIMAGE, YTD, and Q1GOAL properties:protected function
createButton_clickHandler(event:MouseEvent):void
{
var employee:EmployeeSalesData = new
EmployeeSalesData();
employee.SALESTARGETID = "100039";
employee.FIRSTNAME = "New";
employee.LASTNAME = "New";
employee.PHONE = "000-000-0000";
employee.EMAIL = "New";
employee.REGION = "New";
employee.EMPIMAGE = "New";
employee.Q1GOAL = 0;
employee.YTD = 0;
}
You have just populated the new instance of the EmployeeSalesData object with new data.
Note: For the purposes of this example, you are hard-coding the SALESTARGETID property.
createButton_clickHandler() function, attach the newly created employee information to the DataGrid control's dataProvider property.protected function createButton_clickHandler(event:MouseEvent):void
{
...
employee.YTD = 0;
employeeDataGrid.dataProvider.addItem(employee);
}
The dataProvider property of a DataGrid control contains a complex object that contains all of the EmployeeSalesData instances for all the records. The addItem() method of the DataGrid control will add the specified item, in this case employee, to the end of the data.
verticalScrollPosition property to adjust for the new row being added to the control.protected function
createButton_clickHandler(event:MouseEvent):void
{
...
employeeDataGrid.dataProvider.addItem(employee);
employeeDataGrid.verticalScrollPosition = employeeDataGrid.dataProvider.length - 1;
}
The dataProvider property has a length value that contains the number of records in the DataProvider control. ActionScript starts counting at zero, so you must subtract 1 from the length to get the index of the last record. By setting this calcuation for the verticalScrollPosition property you are forcing the scrollbar for the DataGrid control to move to the bottom when you add the new record.
createButton_clickHandler(event) (see Figure 16).
In the following steps, you will create a click handler that commits all changes to the DataGrid control to the server.
Note: Flash Builder will generate an event handler function and switch you to the Source view.
//TODO Auto-generated method stub comment with a call to the commit() function of the F4CF_ManagingData_Service as shown in the following snippet:protected function
saveAllButton_clickHandler(event:MouseEvent):void
{
f4CF_Getting_Started_Service.commit();
}
Remember that, earlier, you created the data service named F4CF_Managing_Data_Service. Flash Builder generated all the code necessary for that data service to function. You reference the service in ActionScript with the same name as your data service, but with an initial lowercase letter. This is common practice for naming instances.
The commit() function is a built-in function of the data service that will run the service that you defined as the update operation in the Data Management wizard. In this case, it is running the updateItem() function once for each record in the DataGrid control.
You should see that your new record has been added to the database.
In the following steps, you will create a click handler for the Delete Button control that will be used to delete records from the DataGrid control.
Note: Flash Builder will generate a click handler function and switch you to the Source view.
deleteButton_clickHandler() function, use the deleteItem() service operation to delete the selected row from the DataGrid control employeeDataGrid using the primary key, SALESTARGETID:protected function
deleteButton_clickHandler(event:MouseEvent):void
{
f4CF_Getting_Started_Service.deleteItem(employeeDataGrid.selectedItem.SALESTARGETID);
}
When you refresh the browser, you will see that the record has been removed from the database.
In this tutorial you learned how to create client-to-server CRUD functionality using the Flash Builder Data Services and Data Management wizards. To learn more about how Flex and ColdFusion work together, refer to the following tutorials: