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 3.1: Creating an event and dispatching the event object

by Trilemetry

Trilemetry
  • Trilemetry, Inc.

Content

  • Create an event type for the Event class
  • Instantiate and dispatch the event to the main application
  • Handle the event in the main application

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 1.2: Creating a user interface
  • Exercise 1.3: Generating an email address using data binding
  • Exercise 2.1: Handling a user event
  • Exercise 2.2: Using the event object

User level

All

Required products

  • Flash Builder 4.5 Premium (Download trial)

Exercise files

  • ex3_01_starter.zip
  • ex3_01_solution.zip

In this exercise you will create an application that lets administrators choose an employee to feature on the Employee of the Month panel of the Employee Portal.

The application involves two custom components: one to choose the employee and write a congratulatory message and the other to preview how the Employee of the Month panel will appear on the Employee Portal (see Figure 1).

Review your task for this exercise.
Figure 1. Review your task for this exercise.

The Preview custom component will initially be invisible. When the user clicks the Preview button in the Choose custom component, an event is dispatched to the main application that will make the Preview component visible. In the next exercise you will populate the Preview component with data from the Choose component.

In this exercise you will learn how to:

  • Create an event type for the Event class
  • Instantiate and dispatch the event to the main application
  • Handle the event in the main application

Create an event type for the Event class

You have used many event types in this video training series. For example, you have used the click event type for a Button control and the change event for the DropDownList control.

Remember that the Flex framework classes can be modified and extended for your application needs. In this section, you will create your own showPreview event type within the Choose custom component.

  1. Download the ex3_01_starter.zip file if you haven't already and extract the file ex3_01_starter.fxp to your computer.
  2. Open Flash Builder.
  3. Import the ex3_01_starter.fxp file.
  4. Open the ex3_01_starter.mxml file.
  5. Run the application.

    Figure 2 shows the application. The DropDownList control is populated with XML data and you can type a message into the TextArea control. The Employee of the Month panel does not show any content and nothing happens yet when you click the Preview button.

Run the application to see its contents.
Figure 2. Run the application to see its contents.
  1. Return to the ex3_01_starter.mxml file in Flash Builder.

    You can see that the main application file defines an HTTPService call to retrieve data from the employees.xml file in the data folder and then populates a class property named employeeData with the returned data. The employeeData object is then passed to the chooseEmployee instance of the Choose custom component, which then binds the data to the DropDownList control.

    The Preview.mxml custom component expects two properties: employeeInfo and message. The values of these properties are bound to the image and text controls in the component. However, since this data is not yet provided, the display appears empty.

  2. Locate the Preview custom component instance in the ex3_01_starter.mxml main application code.
<s:HGroup gap="30"> <components:Choose id="chooseEmployee" employeeData="{employeeData}" x="30" y="90"/> <components:Preview id="previewEmployeeOfTheMonth" title="Employee of the Month" x="250" y="90" width="250"/> </s:HGroup>
  1. To the components:Preview tag, assign the visible property value to a value of false:
<components:Preview id="previewEmployeeOfTheMonth" title="Employee of the Month" x="250" y="90" width="250" visible="false"/>

    This code will make the Preview instance invisible at application start-up. The event that you will create, dispatch, and handle in the rest of this exercise will make the Preview instance visible again.

  1. Save the file.
  2. From the Package Explorer view, open the Choose.mxml file in the components directory.
  3. Locate the Metadata comment.
  4. Below the comment, create a Metadata tag block.
<!-- Metadata ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> <fx:Metadata> </fx:Metadata>
  1. Within the Metadata tag block, type [Ev to invoke the content assist tool and select the Event directive. In the generated template, name the event type showPreview and leave the default type value for the flash.events.Event class.
<fx:Metadata> [Event(name="showPreview", type="flash.events.Event")] </fx:Metadata>

Keep in mind that this is an arbitrary name that you have created for your custom component event. In other words, you've created your own event type for the built-in Event class.

The Metadata tag is a compiler directive that registers this new event type with Flash Builder. If you save Choose.mxml and return to the main application file and use code hinting to see the properties, methods, and events in the component instance, you will see the showPreview event listed.

Instantiate and dispatch the event to the main application

The application scenario includes the administrator selecting an employee from the DropDownList control in the Choose custom component instance, typing a congratulatory message to the user, and then clicking the Preview button. This means that you must handle the click event on the Preview button.

When a user clicks the Preview button, you will handle the click event by creating an instance of the event object you defined in the Metadata compiler directive and then dispatching the event to the main application using the dispatch() method of the Event class.

  1. In the Choose.mxml file, locate the Button control with the id property value assigned to preview.
  2. To the Button control, use the content assist window to add a click event property (see Figure 3).
Generate a click event for the Button control.
Figure 3 . Generate a click event for the Button control.
  1. Using content assist to generate the click event will present you with the Generate Click Handler option in the content assist window (see Figure 4). Click the option or press the Enter key to use the option to generate a function.
Generate a click handler function.
Figure 4 . Generate a click handler function.
  1. Within the Script tag block, below the event handlers comment, locate the generated preview_clickHandler() function.
// event handlers ------------------------------------------- protected function preview_clickHandler(event:MouseEvent):void { // TODO Auto-generated method stub }

    Remember that generated event handlers are named first by the object's id value (in this case preview), then the event type (in this case click), and then the word Handler.

    Handling the click event is not the learning point of this exercise. You have created and handled many events already in the course of this video training series. For the purpose of this exercise, you will create an event object and dispatch it.

  1. Within the preview_clickHandler() function, delete the generated stub comment and declare a variable named eventObject and data type it to the Event class.
protected function preview_clickHandler(event:MouseEvent):void { var eventObject:Event }

    Note: The name of this event object, eventObject, is arbitrary. Remember, though, that it is an instance of the Event class.

  1. Instantiate eventObject using the new keyword and assign it to the showPreview event type that you named in the Metadata tag block.
protected function preview_clickHandler(event:MouseEvent):void { var eventObject:Event = new Event("showPreview"); }

    You have just created an event object that is typed to your own showPreview event type. You do not need to import the Event class.

  1. Within the preview_clickHandler() function, below the eventObject variable instantiation, use the dispatchEvent() method to dispatch eventObject.
protected function preview_clickHandler(event:MouseEvent):void { var eventObject:Event = new Event("showPreview"); dispatchEvent(eventObject); }

    The dispatchEvent() method dispatches the event object to the main application. You will handle the event in the main application file next.

  1. Save the file.

Handle the event in the main application

In this section, you will create an event handler function to handle a dispatch of the showPreview event within the main application file.

  1. Open the ex3_01_starter.mxml file.
  2. Locate the HGroup container that nests the Choose and Preview components tags.
<s:HGroup gap="30"> <components:Choose id="chooseEmployee" employeeData="{employeeData}" x="30" y="90"/> <components:Preview id="previewEmployeeOfTheMonth" title="Employee of the Month" x="250" y="90" width="250" visible="false"/> </s:HGroup>

    You can see that the Choose and Preview instances are named chooseEmployee and previewEmployeeOfTheMonth, respectively.

  1. On the components:Choose tag, use the content assist tool to add the showPreview event (see Figure 5).
Generate the showPreview event.
Figure 5. Generate the showPreview event.

    The Metadata compiler directive has directed Flash Builder to treat the showPreview event type that you created as any other event of the Choose component.

  1. Using the content assist tool to generate the showPreview event will present you with the Generate Click Handler option, within the content assist window (see Figure 6). Select the option or press Enter to generate the function.
Generate the event handler function
Figure 6 . Generate the event handler function
  1. Within the Script block, below the event handlers comment, locate the generated chooseEmployee_showPreviewHandler() function.
protected function chooseEmployee_showPreviewHandler(event:Event):void { // TODO Auto-generated method stub }
  1. In the chooseEmployee_showPreviewHandler() function, delete the generated stub code and assign the previewEmployeeOfTheMonth Preview custom component's visible property to true.
protected function chooseEmployee_showPreviewHandler(event:Event):void { previewEmployeeOfTheMonth.visible = true; }

    You have just dispatched the showPreview event from the Choose component and handled it in the main application in the chooseEmployee_showPreviewHandler() event handler.

  1. Save the file.
  2. Run the application.

    Note that the Employee of the Month panel container is not visible (see Figure 7).

Run the application and note that the Employee of the Month panel container is not visible.
Figure 7. Run the application and note that the Employee of the Month panel container is not visible.
  1. Click the Preview Button control.

    The Employee of the Month panel container is now visible (see Figure 8).

Click the Preview button control to display the custom component.
Figure 8 . Click the Preview button control to display the custom component.

In this exercise you learned how to create and trigger an event, dispatch it to the main application, and handle the event. In the next exercise you will pass data from the Choose component instance to the Preview component instance.

More Like This

  • Exercise 1.5: Experimenting with container layouts
  • Exercise 1.6: Creating MXML custom components with ActionScript properties
  • Exercise 5.2: Defining selector styles
  • Exercise 1.1: Setting up your project files
  • 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 4.7: Creating and navigating application states
  • Exercise 3.4: Passing data to the server with the HTTPService class
  • Exercise 3.4: Passing data to the server with the WebService class

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