2 May 2011
Beginning
In this exercise you will learn how to use the HTTPService object's result event to store data in a variable that is bound to Flex controls.
Figure 1 shows employees listed in a DropDownList control at the top of the Employee Portal: Vehicle Request Form. The data for the selected user, which is retrieved from the database via an HTTPService object call, is bound to the office phone number field.
In this section you will review the Employee Portal: Vehicle Request Form that you built in Day 1 of this training series.
The Office Phone field populates with the selected employee's phone number (see Figure 2).
lastResult property of the service object is bound to the DropDownList control and the office phone number field.Using the lastResult property of the service object is a very quick way to display data. However, if you want to manipulate the data first, or place it into a reusable variable, you should use the result event to handle the data and then bind it to the controls.
In this section you will define the result event handler for the HTTPService object.
Declarations block, locate the HTTPService object.result event (see Figure 3).The content assist tool is activated by placing your cursor inside the MXML tag (but outside any existing properties) and pressing the spacebar. If you place each of your property/value pairs on a new line of code (see Figure 3) and want to activate the content assist tool, simply, type Ctrl+spacebar. In either case, start typing the name of the desired property or event, in this case result, to see that value selected.
Note: The lightening bolt icon indicates an event while the green circle indicates a property. The three square blocks represent a style, the light purple shape with the dots in the four corners represents an effect, and the maroon circle with an "M" in the middle represents metadata.
Using the content assist tool to generate the result event displays the Generate Result Handler option (see Figure 4).
Script block.import statement and employeeService_resultHandler() function:import mx.rpc.events.ResultEvent;
...
protected function employeeService_resultHandler(event:ResultEvent):void
{
// TODO Auto-generated method stub
}
Note: In order to complete this part of the exercise, you must have Flash Builder Premium Edition installed. If you have Flash Builder Standard Edition installed, skip this section and continue with the next section. If you want to download and install a trial version of Flash Builder Premium Edition, be sure to 1) back up your existing workspace and projects, 2) uninstall Flash Builder Standard Edition, and then 3) install the trial version of Flash Builder Premium Edition. To compare the Standard Edition with the Premium Edition, check out the Upgrade Details page.
Before you handle the data in the result handler you just created, you will make sure that the request and response for data were properly made. You will use the Network Monitor view to examine the request being made by Flash Builder and the retrieved XML data from the server.
Note: If the workspace you are using has multiple projects open, you may not see the recording in the Network Monitor. Close all other projects and enable the monitor again for your current project and make sure the message in the Network Monitor view reads, "Enabled for project 'ex2_04_starter."
Now that you have verified that the server request and response have been properly sent and received, you will use the Flash Builder Debugger to examine the data that is in your Flex application.
Script block, locate the employeeService_resultHandler() function.employeeService_resultHandler() function to add a breakpoint (see Figure 9).
Remember that the event object inside the employeeService_resultHandler() function is only available within that function. To access the data outside the function, you will place the returned data into an ArrayCollection instance.
Script block.Script block, below the import statements, use the content and quick assist tools to create a Bindable private variable named employees and use the content assist tool to data type it to the ArrayCollection class (see Figure 14). You should also see an import statement for the ArrayCollection class created within the Script block.
employeeService_resultHandler() function.employees variable (the new instance of the ArrayCollection class). Use event.result.employees.employee to reference the repeating node that you saw in the Flash Builder Debugger.protected function employeeService_resultHandler(event:ResultEvent):void
{
employees = event.result.employees.employee;
}
components comment, locate the Form container.dataProvider property value so that it is bound to the employees class variable:<s:FormItem label="Employee:">
<s:DropDownList id="dropDownList"
labelField="lastName"
dataProvider="{employees}"/>
</s:FormItem>
There is no visual change to the application; the application runs as it did at the beginning of the exercise (see Figure 15).
In this exercise you learned how to use the HTTPService object's result event to store data in a variable and how to map the data to Flex controls using the variable. In the next exercises you learn how to populate an application with data and handling faults, using HTTPService, RemoteService, and WebService components.
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.