2 May 2011
Beginning
In this exercise, you will use the RemoteObject component to send data to a server to insert it into the database. You will also handle the confirmation message from the server to display in an alert dialog box (see Figure 1).
Remember that, in a previous exercise, you consolidated the form data in a value object and dispatched it from a custom component using a custom event.
In this exercise, you will learn how to:
In this section you will create a RemoteObject instance that will be used to send information to the server.
Declarations block, below the RemoteObject instance, create another RemoteObject instance.<fx:Declarations>
<s:RemoteObject id="employeeService"
destination="ColdFusion"
source="f4iaw100.remoteData.employeeData"
endpoint="http://adobetes.com/flex2gateway/"
result="employeeService_resultHandler(event)"/>
<s:RemoteObject />
</fx:Declarations>
RemoteObject instance, add the id property with a value of vehicleService, the destination property with a value of ColdFusion, the source property with a value of f4iaw100.remoteData.vehicleRequestData and the endpoint property with a value of http://adobetes.com/flex2gateway/.<s:RemoteObject id="vehicleService"
destination="ColdFusion"
source="f4iaw100.remoteData.vehicleRequestData"
endpoint="http://adobetes.com/flex2gateway/"/>
Note: This example generates the XML using an Adobe ColdFusion server, but you can use any server to generate the XML file. Refer to the Flex and ColdFusion, PHP, Java, or .NET integration pages for more information.
In this section you will add the code to make the RemoteObject request and transfer the Form container data, as a value object dispatched from a custom component using a custom event, to the server.
VehicleRequest() class definition function, type [Rem to invoke the content assist tool and press Enter to insert the RemoteClass metadata tag into your code.package valueObjects
{
[RemoteClass(alias=”alias”)]
public class VehicleRequest
alias property to f45iaw100.remoteData.VehicleRequest. This will map the VehicleRequest value object to the f45iaw100.remoteData.VehicleRequest Java object on the ColdFusion server.package valueObjects
{
[RemoteClass(alias="f45iaw100.remoteData.VehicleRequest")]
public class VehicleRequest
{
public var id:String;
public var mobilePhone:String;
public var phone:String;
public var pickupDate:String;
public var returnDate:String;
public var addStatus:String;
...
component:VehicleRequestForm instance below the Declarations block.vehicleRequestEvent property with a value of vehiclerequestform1_vehicleRequestEventHandler(event).The vehiclerequestform1_vehicleRequestEventHandler() function was created in Exercise 3.3 (Dispatching a value object from the custom component) to handle the vehicleRequestEvent custom event in the main application.
Script block, locate the vehiclerequestform1_vehicleRequestEventHandler() function.Alert.show() method.vehicleService.addVehicleRequest() method to pass the argument data set to the event.vehicleRequestData value.The vehicleService.addVehicleRequest() method references the addVehicleRequest() cffunction located in the employeeData.cfc that the RemoteObject component is calling. The addVehicleRequest() cffunction requires the argument data, which is why you have to send the event.vehicleRequestData value object using the data parameter.
protected function vehiclerequestform1_vehicleRequestEventHandler(event:VehicleRequestEvent):void
{
vehicleService.addVehicleRequest({data:event.vehicleRequestData});
}
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 Flash Builder Network Monitor to analyze the result of the vehicleService RemoteObject instance.
vehicleService RemoteObject object, use the content assist tool to generate the result event (see Figure 2).
Script block, locate the vehicleService_resultHandler() function and delete the generated code stub.You should see an error (see Figure 4).
Note the red circle with an X in it, signifying an error has occurred.
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 'ex3_04_remote_starter.'"
Next, you will add a fault handler to show an Alert message when the Mobile Phone field is left blank.
vehicleService RemoteObject object, add a fault event and, using the content assist tool, generate the fault handler.vehicleService_faultHandler() method in the Script block and delete the generated code stub.Alert.show() method to display the event.fault.faultString value as the text in the Alert pop-up.protected function vehicleService_faultHandler(event:FaultEvent):void
{
Alert.show(event.fault.faultString,"Fault Information for addVehicleRequest");
}
In this section you will use the RemoteObject component's result event and the addStatus value to determine if the value object has been successfully written to the data table on the server.
vehicleService_resultHandler() function.if statement.protected function
vehicleService_resultHandler(event:ResultEvent):void
{
if()
{
}
}
if statement to evaluate if the event.result.id value.protected function
vehicleService_resultHandler(event:ResultEvent):void
{
if(event.result.id)
{
}
{
if statement, use the Alert.show() method to display the text: The request was submittedevent.result.id value.protected function vehicleService_resultHandler(event.ResultEvent):void
{
if(event.result.addStatusid)
{
Alert.show("The request was submitted. The record id is " + event.result.id);
}
}
You should see the Alert window display the message that your request was submitted successfully and you should also see the record id (see Figure 9).
In this exercise, you learned how to use the RemoteObject component to send data to a server to insert it into the database. You also learned how to handle the confirmation message from the server to display an alert dialog box.