2 May 2011
Beginning
In the previous two exercises you used the HTTPService and WebService components to access dynamic server-side data. Both components return untyped data. If you want to work with typed data, use the RemoteObject component.
In this exercise you will use the RemoteObject object to retrieve the dynamic server-side data to populate the Employee DropDownList control in the Employee Portal: Vehicle Request application shown in Figure 1.
In this exercise you will learn how to:
In this section you will use the RemoteObject component to retrieve the data for the Employee Portal: Vehicle Request application.
Note: In this exercise, you are using the remoting gateway for a ColdFusion server. However, you can use any server technology that supports binary AMF data. Some examples are BlazeDS for Java and the Zend Framework for PHP. Explore the mini Flex Developer Centers for PHP, Java, ColdFusion and .NET to learn more about Flex and those technologies.
Declarations block.Declarations tag set, create a RemoteObject instance:<fx:Declarations>
<s:RemoteObject/>
</fx:Declarations>
RemoteObject component instance, add the id property with a value of employeeService, the destination property with a value of ColdFusion, the source property with a value of f45iaw100.remoteData.employeeData, and the endpoint property with a value of http://adobetes.com/flex2gateway/.By setting the source property to f45iaw100.remoteData.employeeData, you are pointing to the ColdFusion component that the RemoteObject instance will use to query the database on the server. The endpoint value, http://adobetes.com/flex2gateway/, refers to the mapping used by the ColdFusion server to handle AMF/RemoteObject requests.
<s:RemoteObject id="employeeService"
destination="ColdFusion"
source="f45iaw100.remoteData.employeeData"
endpoint="http://adobetes.com/flex2gateway/"/>
In this section you will initialize the retrieval of data using the RemoteObject component.
Script block, locate the initApp() function.employeeService.getEmployees() method.The getEmployees() method exists in the ColdFusion component and returns all of the employee information listed in the table on the server.
private function initApp():void
{
pickupDate.addEventListener(CalendarLayoutChangeEvent.CHANGE, dateChangeHandler);
returnDate.addEventListener(CalendarLayoutChangeEvent.CHANGE, dateChangeHandler);
employeeService.getEmployees();
}
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.
In this section you will use the Network Monitor view to examine the data returned by the RemoteObject object.
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_05_remote_starter.'"
You should see that the data result is displayed in the Network Monitor as shown in Figure 5.
Note: The data properties are all uppercase. ColdFusion returns properties in all upper case format by default. You need to adjust the Flex controls to be compatible with the data.
In this section you will store the retrieved data in an ArrayCollection variable named employees that is bound to the DropDownList control in the Employee Portal: Vehicle Request Form.
RemoteObject tag, use the content assist tool to generate the result event (see Figure 6).
Script block, locate the employeeService_resultHandler() function.employees variable to the event.result property:protected function employeeService_resultHandler(event:ResultEvent):void
{
employees = event.result;
}
as operand to cast the event.result value to the ArrayCollection class.The result of the RemoteObject object is typed to the Object class by default. You must cast the result to the ArrayCollection class so that it can be used with the employees ArrayCollection variable.
protected function employeeService_resultHandler(event:ResultEvent):void
{
employees = event.result as ArrayCollection;
}
In this section you will adjust the application user interface to use the data returned by the service call.
labelField property to LASTNAME.<s:FormItem label="Employee:">
<s:DropDownList id="dropDownList"
dataProvider="{employees}"
labelField="LASTNAME"/>
</s:FormItem>
dropDownList.selectedItem.PHONE:<s:FormItem label="Office Phone:">
<s:TextInput id="phone"
text="{dropDownList.selectedItem.PHONE}"/>
</s:FormItem>
In this section, you will generate a fault event.
In this section, you will use the Flash Builder Debugger to view the information returned from the fault event.
source property, remove the last character so that property will be invalid.<s:RemoteObject id="employeeService"
destination="ColdFusion"
source="f45iaw100.remoteData.employeeDat"
endpoint="http://adobetes.com/flex2gateway"
result="employeeService_resultHandler(event)"
fault="employeeService_faultHandler(event)"/>
employeeService_faultHandler() function.employeeService_faultHandler() function (see Figure 9).
Note the faultString property and value (see Figure 10).
In this section, you will use the Alert class to create a pop-up dialog box to display fault data.
employeeService_faultHandler() function.show() method of the mx.controls.Alert class to display the event.fault.faultString value as text in an Alert pop-up. Make the title of the alert Fault Information. Within the Script block, note the import statement for the mx.controls.Alert class. If this import statement is not present, add it to your code to make the application work.
protected function employeeService_faultHandler(event:FaultEvent):void
{
Alert.show(event.fault.faultString,"Fault Information");
}
You will see the Alert dialog box display the fault event information (see Figure 11).
In this exercise you learned how to use a RemoteObject object to retrieve data from a remote ColdFusion server.
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.