Adobe
Products
Acrobat
Creative Cloud
Creative Suite
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
Student and Teacher Editions
More products
Solutions
Digital marketing
Digital media
Education
Financial services
Government
Web Experience Management
More solutions
Learning Help Downloads Company
Buy
Home use for personal and home office
Education for students, educators, and staff
Business for small and medium businesses
Licensing programs for businesses, schools, and government
Special offers
Search
 
Info Sign in
Welcome,
My cart
My orders My Adobe
My Adobe
My orders
My information
My preferences
My products and services
Sign out
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Adobe
Products Sections Buy   Search  
Solutions Company
Help Learning
Sign in Sign out My orders My Adobe
Preorder Estimated Availability Date. Your credit card will not be charged until the product is shipped. Estimated availability date is subject to change. Preorder Estimated Availability Date. Your credit card will not be charged until the product is ready to download. Estimated availability date is subject to change.
Qty:
Purchase requires verification of academic eligibility
Subtotal
Review and Checkout
Adobe Developer Connection / Flex Developer Center / Flex in a Week /

Exercise 3.4: Passing data to the server with the RemoteObject class

by Trilemetry

Trilemetry
  • Trilemetry, Inc.

Content

  • Create the RemoteObject instance
  • Initialize the data transfer
  • Use the Network Monitor to view
  • Handle the result event

Modified

2 May 2011

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
Flash Builder Flex RIA

Requirements

Prerequisite knowledge

  • Exercise 1.1: Setting up Flash Builder and your project files
  • Exercise 2.5: Retrieving and handling data with RemoteObject
  • Exercise 3.3: Dispatching a value object from the custom component

User level

Beginning

Required products

  • Flash Builder 4.5 Premium (Download trial)

Exercise files

  • ex3_04_remote_starter.zip
  • ex3_04_remote_solution.zip

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.

Preview the application
Figure 1. Preview the application

In this exercise, you will learn how to:

  • Create the RemoteObject instance
  • Initialize the data transfer
  • Use the Network Monitor to view
  • Handle the result event

Create the RemoteObject instance

In this section you will create a RemoteObject instance that will be used to send information to the server.

  1. Download the ex3_04_remote_starter.zip file if you haven't already and extract the ex3_04_remote_starter.fxp to your computer.
  2. Open Flash Builder.
  3. Import the ex3_04_remote_starter.fxp project file.
  4. From the Package Explorer view, open the ex3_04_remote_starter.mxml file.
  5. Within the Declarations block, below the RemoteObject instance, create another RemoteObject instance.
<fx:Declarations> <s:RemoteObject id="employeeService" destination="ColdFusion" source="f45iaw100.remoteData.employeeData" endpoint="http://adobetes.com/flex2gateway/" result="employeeService_resultHandler(event)"/> <s:RemoteObject /> </fx:Declarations>
  1. To the new 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 f45iaw100.remoteData.vehicleRequestData and the endpoint property with a value of http://adobetes.com/flex2gateway/.
<s:RemoteObject id="vehicleService" destination="ColdFusion" source="f45iaw100.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.

  1. Save the file.

Initialize the data transfer

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.

  1. Open the VehicleRequest.as file from the valueObjects package in the Package Explorer view.
  2. Within the VehicleRequest.as file, above the VehicleRequest() class definition function, type [Rem to invoke the content assist tool and press Enter to insert the RemoteClass metadata tag into your code.
  3. Place your cursor inside the closing bracket of the RemoteClass tag, type an opening parentheses and press Ctrl+Space to invoke the content assist tool. You should see the following:
package valueObjects { [RemoteClass(alias=”alias”)] public class VehicleRequest
  1. Change the value of the 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; ...
  1. Save the file.
  2. Switch to the ex3_04_remote_starter.mxml file.
  3. Locate the component:VehicleRequestForm instance below the Declarations block.
  4. Note the 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.

  5. Within the Script block, locate the vehiclerequestform1_vehicleRequestEventHandler() function.
  6. Within the function, delete the Alert.show() method.
  7. Use the 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}); }
  1. Save the file.

Use the Network Monitor to view

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. 

  1. For the vehicleService RemoteObject object, use the content assist tool to generate the result event (see Figure 2).
Use the content assist tool to generate the result event.
Figure 2. Use the content assist tool to generate the result event.
  1. Use the content assist tool to generate a handler function for the result event (see Figure 3).
Generate a result handler function.
Figure 3. Generate a result handler function.
  1. Within the Script block, locate the vehicleService_resultHandler() function and delete the generated code stub.
  2. Save the file.
  3. Open the Network Monitor.
  4. Click the Enable Monitor button.
  5. Run the application.
  6. Select an employee, a pickup date and a return date, leave the Mobile Phone field empty and click Submit Request.

    You should see an error (see Figure 4).

Figure 4. An error is displayed when you submit the form.
Figure 4. An error is displayed when you submit the form.
  1. Return to Flash Builder.
  2. Double-click the Network Monitor tab to maximize it.
  3. Select the addVehicleRequest operation (see Figure 5).

    Note the red circle with an X in it, signifying an error has occurred.

Figure 5. Select the addVehicleRequest operation.

    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.'"

Figure 6. Switch from Tree View to Raw View.
Figure 6. Switch from Tree View to Raw View.
  1. Locate the message that reads: Field 'VEHICLEREQUESTS.MOBILEPHONE' cannot be a zero-length string (see Figure 7).
Figure 7. Locate the error message in the Response tab of the Network Monitor.
Figure 7. Locate the error message in the Response tab of the Network Monitor.

    Next, you will add a fault handler to show an Alert message when the Mobile Phone field is left blank.

  1. Double-click the Network Monitor tab to minimize the view.
  2. To the vehicleService RemoteObject object, add a fault event and, using the content assist tool, generate the fault handler.
  3. Locate the vehicleService_faultHandler() method in the Script block and delete the generated code stub.
  4. Within the function add the Alert.show() method to display the event.fault.faultString value as the text in the Alert pop-up.
  5. Make the title of the alert message: Fault Information for addVehicleRequest.
protected function vehicleService_faultHandler(event:FaultEvent):void { Alert.show(event.fault.faultString,"Fault Information for addVehicleRequest"); }
  1. Save the file.
  2. Run the application.
  3. Fill out everything except the Mobile Phone field in the form and click Submit Request.
  4. You should see the alert message displayed (see Figure 8).
Figure 8. View the alert message displaying the faultString value.
Figure 8. View the alert message displaying the faultString value.

Handle the result event

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.

  1. Locate the vehicleService_resultHandler() function.
  2. Within the function, use content assist to insert a code template for an if statement.
protected function vehicleService_resultHandler(event:ResultEvent):void { if() { } }
  1. Set the if statement to evaluate if the event.result.id value.
protected function vehicleService_resultHandler(event:ResultEvent):void { if(event.result.id) { } {
  1. Within the if statement, use the Alert.show() method to display the text: The request was submitted. The record id is and add the event.result.id value.
protected function vehicleService_resultHandler(event.ResultEvent):void { if(event.result.id) { Alert.show("The request was submitted. The record id is " + event.result.id); } }
  1. Save the file.
  2. Run the application.
  3. From the DropDownList control, choose an employee.
  4. In the Mobile Phone field, type 555-555-5555.
  5. Choose a Pickup Date and a Return Date, then click the Submit Request button.

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).

View the message in the Alert window.
Figure 9. View the message in the Alert window.

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.

More Like This

  • Exercise 5.9: Skinning the SkinnableDataContainer container
  • Exercise 5.10: Creating a production build
  • Exercise 5.7: Creating a vertical title bar on a Panel container
  • Exercise 1.1: Setting up your project files
  • Exercise 1.6: Creating MXML custom components with ActionScript properties
  • Exercise 2.1: Handling a user event
  • Exercise 2.2: Using the event object
  • Exercise 2.3: Using the addEventListener() method
  • Exercise 2.4: Populating an ArrayCollection with retrieved data using the result event
  • Exercise 4.5: Creating and using item renderers and item editors

Products

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • Mobile Apps
  • Photoshop
  • Touch Apps
  • Student and Teacher Editions

Solutions

  • Digital marketing
  • Digital media
  • Web Experience Management

Industries

  • Education
  • Financial services
  • Government

Help

  • Product help centers
  • Orders and returns
  • Downloading and installing
  • My Adobe

Learning

  • Adobe Developer Connection
  • Adobe TV
  • Training and certification
  • Forums
  • Design Center

Ways to buy

  • For personal and home office
  • For students, educators, and staff
  • For small and medium businesses
  • For businesses, schools, and government
  • Special offers

Downloads

  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player

Company

  • News room
  • Partner programs
  • Corporate social responsibility
  • Career opportunities
  • Investor Relations
  • Events
  • Legal
  • Security
  • Contact Adobe
Choose your region United States (Change)
Choose your region Close

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy Policy and Cookies (Updated)

Ad Choices

Reviewed by TRUSTe: site privacy statement