Requirements      
Prerequisite knowledge Required products
  User level
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:
  • Experienced ColdFusion developers without previous Adobe Flex framework or Adobe Flash Builder 4 experience.
  • ColdFusion developers who are familiar with ColdFusion components.
Adobe ColdFusion Enterprise Edition (2016 release) (Download trial)
Flex Builder (Download trial)
 
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:
 
  • Experienced ColdFusion developers without previous Adobe Flex framework or Adobe Flash Builder 4 experience.
  • ColdFusion developers who are familiar with ColdFusion components.

 
Configuring data return type

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.
 
  1. Locate the Data/Services view in Flash Builder 4. You should see the F4CF_Getting_Started_Service service shown in Figure 1.
Verify whether the EmployeeSalesData data type has been defined.
Figure 1. Verify whether the EmployeeSalesData data type has been defined.
 
  1. Expand the Data Type category. If you see the EmployeeSalesData data type listed (also shown in Figure 1), then you can skip to the next section, Mapping service calls to CRUD functionality.
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.
 
  1. Right-click the getAllData() function in the F4CF_Getting_Started_Service option of the Data/Services view and select Configure Return Type (see Figure 2).
Configure the return type of getAllData() function.
Figure 2. Configure the return type of getAllData() function.
 
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.
 
  1. In the Configure Return Type dialog box, click Next to auto-detect a return type (see Figure 3).
Create a new custom data type.
Figure 3. Create a new custom data type.
 
  1. In the Configure Return Type dialog box, create a new custom data typenamed EmployeeSalesData. (see Figure 4).
Enter the remoting credentials for the getAllData() CFC function.
Figure 4. Enter the remoting credentials for the getAllData() CFC function.
 
  1. Click Finish.
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.
 
The data type and its properties.
Figure 5. The data type and its properties.
 

Mapping service calls to CRUD functionality

 
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.
 
  1. In the Data/Services view, right-click the EmployeeSalesData data type and select Enable Data Management.
  2. In the Select Unique Identifier for the Data Type dialog box, click the check box next to SALESTARGETID (see Figure 6).
Select Unique Identifier for the Data Type dialog box.
Figure 6. Select Unique Identifier for the Data Type dialog box.
 
You must choose the SALESTARGETID as the unique identifier for the EmployeeSalesData data type, because it is the primary key of the database.
 
  1. Click Next.
    In the Map Data Management Operations dialog box, you must select the appropriate service call for each field.
     
  2. For the Create (Add) Operation, select the createItem() service operation from the popup menu.
  3. Select getItem() for the Get Item operation.
  4. Select updateItem() for the Update Item operation.
  5. Select deleteItem() for the Delete Item operation.
     
    Figure 7 shows what your Map Data Management Operations dialog box should look like at this point.
The Map Data Management Operations dialog box.
Figure 7. The Map Data Management Operations dialog box.
 
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.
 
  1. Click Finish.
     
    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.
The EmployeeSalesData data type with data management enabled.
Figure 8. The EmployeeSalesData data type with data management enabled.

 

Retrieving data for display

 

In this section, you will populate a DataGrid control with employee sales information.
 
  1. In the F4CF_Getting_Started project, close all open MXML files.
  2. Create a new application file by selecting File > New > MXML Application from the menu.
  3. Name the file F4CF_Managing_Data.mxml and set the Layout popup list to None.
  4. Click Finish.
    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.
     
  5. Select Design mode and locate the Components view.
  6. Select a DataGrid control and drag it into the Design area (see Figure 9).
Create a DataGrid control using Design mode.
Figure 9. Create a DataGrid control using Design mode.
 
  1. Using the Properties view, enter employeeDataGrid for the ID property of the DataGrid control, and set the Editable property to True (see Figure 10).
Use the Properties view to set the ID property of the DataGrid control.
Figure 10. Use the Properties view to set the ID property of the DataGrid control.
 
  1. In the Data/Services view, drag and drop the 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.
     
Populate the DataGrid control using the getAllData() service call.
Figure 11. Populate the DataGrid control using the getAllData() service call.
 
  1. Leave the defaults in the Bind To Data dialog window, and Click OK (see Figure 12).
Leave the defaults in the Bind To Data dialog window
Figure 12.
 
  1. Save the file and run the application by clicking the Play (round green) button on the toolbar (see Figure 13). Alternatively, you can select Run > F4CF_Managing_Data.
Flash Builder Run button
Figure 13. Flash Builder Run button
 
 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.
 
The DataGrid control populated with employee sales data.
Figure 14. The DataGrid control populated with employee sales data.
 

Applying and handling data service calls on Button controls

 
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.
 
Adding Button controls to the interface
 
In the following steps, you will add Button controls to the application display that users will use to trigger add, delete, and save functionality.
 
  1. Drag and drop three Button controls from the Controls folder of the Components view into the Design area (see Figure 15).
Three Button controls below the DataGrid component
Figure 15. Three Button controls below the DataGrid component
  1. Double-click each Button to change the button label to Create, Delete, and Save All, respectively.
Handling Create button events
 
In the following steps, you will create a click handler for the Create Button control that will add records to the DataGrid control.
 
  1. Right-click the Create Button control and select Generate Click Handler.
    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.
     
  2. Rename the button1_clickHandler() function to createButton_clickHandler().
    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.
     
  3. Replace the //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.
 
  1. Note that when you typed the 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;
  1. Below the variable declaration, define the 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.
 
  1. Below the employee property declarations in the 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.
 
  1. Set the DataGrid control's 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.
 
  1. Switch to Design mode.
  2. Select the Create button.
  3. Use the Properties view to change the value of the On Click property to createButton_clickHandler(event) (see Figure 16).
Figure 16. Set the Create button's On Click property.
Figure 16. Set the Create button's On Click property.
 
 
  1. Save the file and run the application.
  2. Click the Create button.
You should see an entry added to the bottom of the DataGrid control (see Figure 17). Note, however, that you only added the data to the DataGrid control. You did not add the new record to the server.
 
Add a new record to the DataGrid control.
Figure 17. Add a new record to the DataGrid control.
 
Handling Save All button events
In the following steps, you will create a click handler that commits all changes to the DataGrid control to the server.
 
  1. Return to Design mode.
  2. Right-click the Save All Button control and select Generate Click Handler.
    Note: Flash Builder will generate an event handler function and switch you to the Source view.
  3. Rename the generated function saveAllButton_clickHandler().
  4. Replace the //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.
 
  1. Switch to Design mode.
  2. Select the Save All button.
  3. Use the Properties view to change the value of the On Click property to saveAllButton_clickHandler(event).
  4. Save the file and run the application.
  5. Click the Create Button control and add a new record.
  6. Click the Save All Button control to send the changes to the server.
  7. Refresh the browser to refresh the application.
You should see that your new record has been added to the database.
 
Handling Delete button events
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.
 
  1. Return to Design mode.
  2. Right-click the Delete Button control and select Generate Click Handler.
    Note: Flash Builder will generate a click handler function and switch you to the Source view.
     
  3. Rename the generated function deleteButton_clickHandler.
  4. In the 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); }
  1. Return to Design view.
  2. Select the Delete Button control and Locate the Properties view.
  3. Change the On Click property to deleteButton_clickHandler(event).
  4. Save and run the file.
  5. Select the record that you just created earlier and click the Delete button.
When you refresh the browser, you will see that the record has been removed from the database.
 

Where to go from here

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: