Adobe
Products
Acrobat
Creative Cloud
Creative Suite
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
Student and Teacher Editions
More products
Solutions
Creative tools for business
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 /

Exercise 2.4: Populating an ArrayCollection with retrieved data using the result event

by Trilemetry

Trilemetry
  • Trilemetry, Inc.

Content

  • Preview the application
  • Handle the result event
  • Monitor the data request and retrieval using the Network Monitor
  • Examine the result event object using the Flash Builder Debugger
  • Populate an ArrayCollection instance with the retrieved data

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.1: Handling a user event

User level

Beginning

Required products

  • Flash Builder 4.5 Premium (Download trial)

Sample files

  • ex2_04_starter.zip
  • ex2_04_solution.zip

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.

Preview the application.
Figure 1. Preview the application.

In this exercise you will learn how to:

  • Preview the application
  • Handle the result event
  • Monitor the data request and retrieval using the Network Monitor
  • Examine the result event object using the Flash Builder Debugger
  • Populate an ArrayCollection object with the retrieved data

Preview the application

In this section you will review the Employee Portal: Vehicle Request Form that you built in Day 1 of this training series.

  1. Download the ex2_04_starter.zip file if you haven't already and extract the ex2_04_starter.fxp to your computer.
  2. Open Flash Builder.
  3. Import the ex2_04_starter.fxp file.
  4. Within the ex2_04_starter project, open the ex2_04_starter.mxml file.
  5. Run the application.
  6. Select an employee last name from the DropDownList control.

    The Office Phone field populates with the selected employee's phone number (see Figure 2).

Choose an employee from the DropDownList control.
Figure 2. Choose an employee from the DropDownList control.
  1. Return to Flash Builder and examine the code in the ex2_04_starter.mxml file.You should see the HTTPService object that defines the XML file to be retrieved for the application data. The lastResult property of the service object is bound to the DropDownList control and the office phone number field.

Handle the result event

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.

  1. Return to Flash Builder.
  2. Within the Declarations block, locate the HTTPService object.
  3. Within the HTTPService object tag, use the content assist tool to generate the 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.

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

Use the content assist tool to generate the result event for the HTTPService object.
Figure 3. Use the content assist tool to generate the result event for the HTTPService object.

    Using the content assist tool to generate the result event displays the Generate Result Handler option (see Figure 4).

  1. Click the menu option or hit the Enter key to generate the result handler function.
Use the content assist tool to generate a result handler function for the HTTPService object.
Figure 4. Use the content assist tool to generate a result handler function for the HTTPService object.
  1. Locate the Script block.
  2. Note the generated import statement and employeeService_resultHandler() function:
import mx.rpc.events.ResultEvent; ... protected function employeeService_resultHandler(event:ResultEvent):void { // TODO Auto-generated method stub }

Monitor the data request and retrieval using the Network Monitor

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.

  1. Open the Network Monitor view (see Figure 5).
Open the Network Monitor view.
Figure 5. Open the Network Monitor view.
  1. Click the Enable Monitor button (see Figure 6).
Turn on the Network Monitor.
Figure 6. Turn on the Network Monitor.
  1. Save the file and run the application.
  2. Return to Flash Builder and double-click the Network Monitor view's tab to maximize the view.
  3. Select the recording of the HTTPService object (see Figure 7).
  4. 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."

Select the HTTPService recording.
Figure 7. Select the HTTPService recording.
  1. On the right side, within the Response tab, expand the Response body tree to reveal the employee data (see Figure 8).
Expand the Response body tree to reveal the employee data.
Figure 8. Expand the Response body tree to reveal the employee data.
  1. Double-click the Network Monitor view's tab to minimize the view.

Examine the result event object using the Flash Builder Debugger

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.

  1. Within the Script block, locate the employeeService_resultHandler() function.
  2. Double-click the Editor gutter on the same line as the closing brace of the employeeService_resultHandler() function to add a breakpoint (see Figure 9).
Place a breakpoint on the ending line of the employeeService_resultHandler() function.
Figure 9. Place a breakpoint on the ending line of the employeeService_resultHandler() function.
  1. Click the Debug button to debug the application (see Figure 10).
Click the Debug button.
Figure 10. Click the Debug button.
  1. The browser will open and return you to Flash Builder. When you see the Confirm Perspective Switch dialog box (see Figure 11), click the Yes button to switch to the Flash Builder Debug perspective.
Click the Yes button to switch to the Flash Builder Debug perspective.
Figure 11. Click the Yes button to switch to the Flash Builder Debug perspective.
  1. Double-click the Variables tab to maximize the view.
  2. Within the Variables view, expand the event object to reveal the result event and the employee data that has been retrieved into the application (see Figure 12). Note the employee node (the repeating node) is automatically an ArrayCollection instance.
Expand the result object to reveal the employees data.
Figure 12. Expand the result object to reveal the employees data.
  1. Double-click the Variables tab to minimize the view.
  2. Click the Terminate button to end the debugging session (see Figure 13).
Terminate the debugging session.
Figure 13. Terminate the debugging session.
  1. Return to the Flash perspective.

Populate an ArrayCollection instance with the retrieved data

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.

  1. Locate the Script block.
  2. Within the 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.

Use the content assist tool to assign a data type to the variable.
Figure 14. Use the content assist tool to assign a data type to the variable.
  1. Locate the employeeService_resultHandler() function.
  2. Within the function, assign the returned data in the event object to the 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; }
  1. Below the UI components comment, locate the Form container.
  2. Within the Form container, locate the FormItem container that nests the DropDownList control.
  3. Reassign the DropDownList control's 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>
  1. Save the file and run the application.

    There is no visual change to the application; the application runs as it did at the beginning of the exercise (see Figure 15).

Run the application to see no change.
Figure 15. Run the application to see no change.

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.

Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License+Adobe Commercial Rights

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.

More Like This

  • Exercise 1.2: Creating an application user interface
  • Exercise 1.3: Generating an email address using data binding
  • Exercise 1.4: Adding data to your application
  • Exercise 1.5: Experimenting with container layouts
  • Exercise 1.6: Creating MXML custom components with ActionScript properties
  • Exercise 1.1: Setting up your project files
  • Exercise 2.5: Retrieving and handling data with the RemoteObject object
  • Exercise 2.6: Separating the model, view, and controller
  • Exercise 2.7: Creating an ActionScript class and instances
  • Exercise 2.8: Creating an ArrayCollection of value objects

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