Getting Started Flex
Getting Started Flex
Getting Started Flex
Trademarks
ActiveEdit, ActiveTest, Add Life to the Web, Afterburner, Aftershock, Andromedia, Allaire, Animation PowerPack, Aria, Attain,
Authorware, Authorware Star, Backstage, Blue Sky Software, Blue Sky, Breeze, Bright Tiger, Captivate, Clustercats, ColdFusion,
Contents Tab Composer, Contribute, Design In Motion, Director, Dream Templates, Dreamweaver, Drumbeat 2000, EDJE,
EJIPT, Extreme 3D, Fireworks, Flash, FlashHelp, Flash Lite, FlashPaper, Flex, Flex Builder, Fontographer, FreeHand, Generator,
Help To Source, HomeSite, Hotspot Studio, HTML Help Studio, JFusion, JRun, Kawa, Know Your Site, Knowledge Objects,
Knowledge Stream, Knowledge Track, LikeMinds, Lingo, Live Effects, MacRecorder Logo and Design, Macromedia,
Macromedia Action!, Macromedia Central, Macromedia Flash, Macromedia M Logo and Design, Macromedia Spectra,
Macromedia xRes Logo and Design, MacroModel, Made with Macromedia, Made with Macromedia Logo and Design, MAGIC
Logo and Design, Mediamaker, Movie Critic, Open Sesame!, RoboDemo, RoboEngine JFusion, RoboHelp, RoboHelp Office,
RoboInfo, RoboInsight, RoboPDF, 1-Step RoboPDF, RoboFlash, RoboLinker, RoboScreenCapture, ReSize, Roundtrip,
Roundtrip HTML, Shockwave, Sitespring, Smart Publishing Wizard, Software Video Camera, SoundEdit, Titlemaker,
UltraDev, Web Design 101, what the web can be, WinHelp, WinHelp 2000, WinHelp BugHunter, WinHelp Find+, WinHelp
Graphics Locator, WinHelp Hyperviewer, WinHelp Inspector, and Xtra are either registered trademarks or trademarks of
Macromedia, Inc. and may be registered in the United States or in other jurisdictions including internationally. Other product
names, logos, designs, titles, words, or phrases mentioned within this publication may be trademarks, service marks, or trade
names of Macromedia, Inc. or other entities and may be registered in certain jurisdictions including internationally.
Third-Party Information
This guide contains links to third-party websites that are not under the control of Macromedia, and Macromedia is not
responsible for the content on any linked site. If you access a third-party website mentioned in this guide, then you do so at your
own risk. Macromedia provides these links only as a convenience, and the inclusion of the link does not imply that Macromedia
endorses or accepts any responsibility for the content on those third-party sites.
Copyright © 2004 Macromedia, Inc. All rights reserved. This manual may not be copied, photocopied, reproduced,
translated, or converted to any electronic or machine-readable form in whole or in part without prior written approval of
Macromedia, Inc. Part Number ZFE15M400
Acknowledgments
Project Management: Stephen M. Gilson
Writing: Matthew J. Horn, Mike Peterson
Editing: Linda Adler, Noreen Maher
Production Management: Patrice O’Neill
Media Design and Production: Adam Barnett, John Francis
Macromedia, Inc.
600 Townsend St.
San Francisco, CA 94103
CONTENTS
INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
3
4 Contents
INTRODUCTION
About Flex Documentation
Getting Started with Flex provides an introduction to Macromedia Flex. This book is intended for
application developers who are new to Flex and require an overview of Flex features and
capabilities.
Contents
Using this manual . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Accessing the Flex documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Chapter Description
Chapter 2, “Using MXML” Describes MXML, the XML language for writing
applications, and how developers use it to create
applications.
Chapter 4, “Architecting Flex Applications” Describes the Flex development process. It describes
coding practices, multitier applications, and application
architecture.
5
Accessing the Flex documentation
The Flex documentation is designed to provide support for the complete spectrum of
participants.
Documentation set
The Flex documentation set includes the following manuals:
Book Description
Getting Started with Flex Contains an overview of Flex features and application
development procedures.
Flex ActionScript and MXML API Reference Provides descriptions, syntax, usage, and code
examples for the Flex API.
This chapter introduces you to the Macromedia Flex presentation server, a development and
runtime environment that lets you create rich interfaces for your web applications. Macromedia
Flex defines a standards-based programming methodology for building the presentation tier of
rich Internet applications. Rich Internet applications combine the responsiveness and richness of
desktop software with the broad reach of web applications to deliver a more effective user
experience.
This chapter describes the characteristics of a typical Flex application, provides an overview of the
Flex feature set, and defines the development process that you use to create Flex applications.
Contents
About Flex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Developing applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
The Flex programming model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Features of Flex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Where to next . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
About Flex
Flex defines a development and runtime environment that lets developers familiar with server-
side technologies build rich front ends for applications that take advantage of Macromedia Flash
Player. While executing in Flash Player, your application can interact with server-side
functionality, such as databases, web services, Java objects, and other server-side services.
Flex brings the power of Flash to developers who are familiar with traditional programming
languages and server-side development environments. In Flex, you use a standards-based
programming model to develop an application, describe the user interface, configure user-
interface controls, and define data bindings.
7
Using Flex in an n-tier application model
By definition, enterprise applications are multitiered, where each tier brings specific benefits to
the application design. A tiered architecture provides natural access points for integration with
existing and future systems.
Each tier serves a specific purpose, and lets you divide the functionality of a business application
into discrete components that you can implement independently from each other. For example,
the following figure shows the tiers of a five-tier application model:
Client Tier
Running on client
Application clients, Flash, applets, and other GUIs
Presentation Tier
ASP, JSP, Servlets, and other UI elements
Business Tier
Connects to business objects
Running on server
Integration Tier
Connects to data and legacy systems
Resource Tier
Databases, external systems, and legacy resources
HTTP/SOAP/AMF
Flex server
Resource tier
In Flex, Flash Player provides the platform for interface development so that both client and
presentation tier logic executes on the client computer. Flex applications that execute in Flash
Player process user interactions; perform data validation; issue HTTP, SOAP, AMF requests; and
perform other operations previously performed on the server. Because Flash Player runs
consistently across all major operating systems and browsers, you do not have to program your
applications for a specific browser or platform.
Note: You can still develop presentation-tier logic for the server as part of your application, and
connect to that logic from the client.
The Flex server executes on a J2EE platform or servlet container, and provides the integration
point for Flex applications to server-side functionality. The Flex server also includes the Flex
compiler, caching mechanism, and web service proxy.
You do not need to modify the business and integration tiers to integrate with Flex. This means
that you code the business and back-end functionality in the same way you did for your existing
applications. Flex lets you connect to your back-end systems and application logic using web
services, HTTP, EJBs, or other methods.
About Flex 9
Applications running in Flash Player behave like desktop applications, instead of a series of linked
pages. Flash Player manages the client interface as a single, uninterrupted flow and does not
require a page load from the server when the client moves from one section of the application
to another.
Flex offers the following additional benefits to your web applications:
Standards-based architecture Macromedia designed Flex, ActionScript, and MXML, the
Flex scripting language, to existing standards. MXML is XML-compliant, implements styles
based on the Cascading Style Sheets, level 1(CSS1) specification, and implements an event model
based on a subset of the W3C DOM Level 3 Events specification. ActionScript is an
ECMAScript-based language that provides support for object-oriented development. The Flex
server executes on standard J2EE platforms or servlet containers.
Reduction in server load and network bandwidth Some or all of the presentation tier of a Flex
application runs in Flash Player, which means that no application server processing resources are
spent on data formatting. By off loading presentation logic to the client, and eliminating the need
for frequent page refreshes, Flex applications minimize network traffic and server load.
Common deployment environment Flex applications deploy on a J2EE server and execute on
Flash Player, which is platform-independent, so customers do not need to install custom client
software. Also, Flash Player runs consistently in all browsers and platforms, so you do not have to
worry about inconsistent behavior in different client environments.
About Flex 11
Direct user feedback Complex tasks must provide feedback to users when the user makes
input errors or enters invalid information. Support for a feedback mechanism should also require
a minimum of server-side interactions so as not to use network bandwidth. Because your
applications execute in Flash Player on the client, the Flex feedback mechanism requires little or
no server-side logic.
Developing applications
Flex supports a component-based development model. You can use the prebuilt components
included with Flex, you can extend the Flex component library by creating new components, or
you can combine prebuilt components to create composite components.
Application model
When you build an application using Flex, you describe its user interface using containers and
controls. A container is a rectangular region of the screen that contains controls and other
containers. Examples of containers are a Form container used for data entry, a Box, and a Grid.
A control is a form element, such as a Button or Text Input field.
For example, the following figure shows two different Box containers, each containing three
Button controls and a ComboBox control:
This figure shows the controls within a horizontal Box (HBox) container. An HBox container
arranges its controls horizontally across the Flash Player drawing surface. The figure also shows
the controls in a vertical Box (VBox) container. A VBox container arranges its controls vertically.
Containers and controls define the application’s user interface. In an MVC design pattern, those
pieces of the application model represent the view. The model is represented by the data model.
Flex data models let you separate your application’s data and business logic from the user
interface.
HTTP/SOAP/AMF
Data binding is the process of tying the data in one object to another object. The data model
supports bidirectional data binding for writing data from Flex controls to the data model, or for
reading data into controls from the model. You can also bind server data to a data model or
directly to Flex controls. For example, you can bind the results returned from a web service to the
data model, and then have that data propagate to your form controls.
The data model supports automatic data validation. This means that you can use the Flex
ZipCode validator to make sure that the value in a model field is a valid ZIP code. If the data is
invalid, you can display a message to the user so that the user can correct the error.
Developing applications 13
Although you can consider a Flex application as part of the View in a distributed MVC
architecture, you can use Flex to implement the entire MVC architecture on the client. A Flex
application has its own view components that define the user interface, model components to
represent data, and controller components responsible for the communication with back-end
systems. This client-side separation of task is not available in HTML.
Upon receiving an HTTP request for an MXML file, Flex performs the following steps:
1. Compiles the MXML file to produce a SWF file.
2. Caches the compiled SWF file on the server.
3. Returns the SWF file to the client.
The SWF file executes in Flash Player. While it is executing, the Flex application makes calls to
server-side services. For example, the application might issue a SOAP request to a web service, use
AMF to connect to a Java object, or make a request to an HTTP service.
Upon subsequent requests to the MXML file, the Flex server determines whether the MXML file
has been modified since the previous request. If not, it returns the same SWF file from the cache.
If the MXML file has been modified, the Flex server recompiles the file and returns an updated
SWF file to the client.
MXML ActionScript
J2EE
The Flex application framework consists of the MXML scripting language, the ActionScript
programming language, and the Flex class library. You write Flex applications using a
combination of MXML and ActionScript. Use MXML to declaratively define the application
user interface elements and use ActionScript to define client logic and procedural control.
The Flex class library contains Flex components, managers, and behaviors. With the Flex
component-based development model, developers can incorporate prebuilt components, create
new components, or combine prebuilt components into composite components.
The Flex runtime services include support for server-side data access, compilation and caching,
enterprise resource integration, and deployment runtime needs. All Flex services minimize
redundancy and fully exploit existing enterprise resources.
</mx:Panel>
</mx:Application>
The first line of this application specifies the XML declaration and must start in line 1, column 1
of the MXML file.
As it is written, this example lays out the user interface, but does not yet contain the application
logic to copy the input text from the TextInput control to the TextArea control. The next section
adds that logic.
The previous example inserts the ActionScript code directly into the MXML code. Although this
technique works well for one or two lines of ActionScript code, for more complex logic you
typically define your ActionScript in an <mx:Script> block, as the following example shows:
<?xml version="1.0"?>
<!-- ?xml tag must start in line 1 column 1 -->
<mx:Script>
<![CDATA[
function duplicate(){
myText.text=myInput.text;
}
]]>
</mx:Script>
</mx:Panel>
</mx:Application>
In this example, you implement the event handler as an ActionScript function. Flex calls this
function in response to the user selecting the Button control. This techniques lets you separate
your MXML code from your ActionScript code. You can also choose to divide your application
into multiple files to increase its modularity. For more information, see Chapter 3, “Using
ActionScript,” on page 43.
Data modeling Defines the • Data model for storing application data.
representation of • Data binding for unidirectional or bidirectional binding.
application data on the to UI controls, and predefined and custom data
client. formatters.
• Data validation to validate data model.
Data Connects to server data • Web services for SOAP messages transmitted over
communication using a set of HTTP. Supports both RPC and document operations,
services communications and concurrent web service requests.
services. • HTTP service for accessing XML over HTTP and
HTTPS.
• Remote Java objects let you call methods on local or
remote objects (Java objects, JavaBeans, EJBs,
JNDI objects) using SOAP or AMF protocols.
Features of Flex 19
Feature Description Details
Behaviors Enhances the user A behavior is a trigger, typically a user action, paired with
experience through an effect:
animation, motion, • Effects include: Dissolve, Fade, Move, Pause, Resize,
sound, and effects. WipeDown, WipeLeft, WipeRight, WipeUp.
• Triggers include: focusIn, focusOut, hide, move, resize,
mouseDown, mouseOver, mouseOut, mouseUp,
show.
Style sheets Standard mechanism for Used for controlling color, font, sizing, and other
declaring styles. attributes of Flex components.
Managers Provides support for • Drag and Drop Manager lets you move data from one
high-level application place in an application to another.
tasks. • Cursor Manager lets you control the cursor image
within your Flex application.
• History Manager lets users navigate through a Flex
application using the web browser’s Back and
Forward navigation commands.
• ToolTipManager displays hints when the user moves
the mouse over a Flex control.
Development Used to develop Flex Debugger Flex Builder
tools applications. ActionScript Profiler
Where to next
This book contains an introduction to Flex and an overview of developing Flex applications. The
Developing Flex Applications book contains detailed information about these topics. For more
information, see the following:
• For information on MXML and ActionScript, see Chapter 2, “Using MXML,” on page 23,
and Chapter 3, “Using ActionScript,” on page 43.
• For information on using Flex components, see Chapter 1, “Using Flex Components,” in
Developing Flex Applications.
• For information on developing custom components, see Chapter 15, “Building an Application
with Multiple MXML Files”, and Chapter 18, “Creating ActionScript Components,” in
Developing Flex Applications.
Where to next 21
22 Chapter 1: Introducing Flex
CHAPTER 2
Using MXML
MXML is an XML language that you use to lay out user-interface components for Macromedia
Flex applications. You also use MXML to declaratively define nonvisual aspects of an application,
such as access to server-side data sources and data bindings between user-interface components
and server-side data sources. This chapter describes MXML and how you use MXML to develop
Flex applications.
Contents
About MXML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Basic MXML syntax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
How MXML relates to standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
About MXML
You use two languages to write Flex applications: MXML and ActionScript. MXML is an XML
markup language that you use to lay out user-interface components. You also use MXML to
declaratively define nonvisual aspects of an application, such as access to data sources on the
server and data bindings between user-interface components and data sources on the server.
ActionScript is an object-oriented programming language, based on the ECMAScript Edition 4
proposal, which you use to write programmatic logic for responding to both user-initiated and
system-initiated events at runtime.
Like HTML, MXML provides tags that define user interfaces. MXML will seem very familiar if
you have worked with HTML. However, MXML is more structured than HTML, and it provides
a much richer tag set. For example, MXML includes tags for visual components such as data
grids, trees, tab navigators, accordions, and menus, as well as nonvisual components that provide
web service connections, data binding, and animation effects. You can also extend MXML with
custom components that you reference as MXML tags.
One of the biggest differences between MXML and HTML is that MXML-defined applications
are compiled into SWF files and rendered by Macromedia Flash Player, providing a richer and
more dynamic user interface than page-based HTML applications do.
MXML does not use concepts specific to Flash, such as the timeline. However, you can use the
Flash authoring environment to create or modify components for use in Flex.
23
You can write an MXML application in a single file or in multiple files. MXML supports custom
components written in MXML files, ActionScript files, and files created using the Flash MX 2004
authoring environment. Some MXML tags, such as the <mx:Script> tag, have a property that
takes a URL of an external file as a value. For example, you can use the source property in an
<mx:Script> tag to reference an external ActionScript file instead of typing ActionScript directly
in the body of the <mx:Script> tag.
Note: You specify a script in the source property of an <mx:Script> tag. You do not specify
ActionScript classes in the source property. For information on using ActionScript classes, see
Chapter 18, “Creating ActionScript Components,” in Developing Flex Applications.
• A path used at runtime that is relative to the context root of the Java web application in which
a Flex application is running; for example:
<mx:HTTPService url="@ContextRoot()/directory/myfile.xml"/>
• A path used at compile-time that is relative to the context root of the Java web application in
which a Flex application is running; for example:
<mx:Script source="/myscript.as"/>
The following example shows a simple “Hello World” application that contains just an
<mx:Application> tag and one child tag, the <mx:Label> tag. The <mx:Application> tag is
always the root tag of a Flex application. The <mx:Label> tag represents a Label control, a very
simple user interface component that displays text.
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" >
<mx:Panel title="My Application" marginTop="10" marginBottom="10"
marginLeft="10" marginRight="10" >
</mx:Panel>
</mx:Application>
To deploy the application, you copy the MXML file to a web application directory. The first time
a user requests the MXML file URL in a web browser, the server compiles the MXML code into a
SWF file. The server then sends the SWF file to the web browser where it is rendered in Flash
Player. Unless the MXML file changes, the SWF file is not recompiled on subsequent requests.
When the Flex compiler autogenerates the HTML file that contains this application, it uses the
height and width properties of the <mx:Application> tag to determine height and width
properties of the <object> and <embed> tags. The <object> and <embed> tags determine the
size of the Flash drawing surface.
The following figure shows the “Hello World” application rendered in a web browser window:
In addition to being the root tag of a Flex application, the <mx:Application> tag represents an
Application container. A container is a user-interface component that contains other components
and has built-in layout rules for positioning its child components. By default, an Application
container lays out its children vertically from top to bottom. You can nest other types of
containers inside an Application container to position user-interface components according to
other rules. For more information, see Chapter 1, “Using Flex Components,” in Developing Flex
Applications.
The properties of an MXML tag, such as the text, color, and fontSize properties of the
<mx:Label> tag, let you declaratively configure the initial state of the component. You can use
ActionScript code in an <mx:Script> tag to change the state of a component at runtime. For
more information, see Chapter 16, “Working with ActionScript in Flex,” in Developing Flex
Applications.
About MXML 25
Laying out a user interface
MXML provides a comprehensive set of components for laying out the user interface of an
application. The standard set of components includes simple form controls, such as buttons and
text fields, as well as components for displaying structured data, such as tree controls, list boxes,
and combo boxes. MXML also provides container components for layout and navigation.
Examples of layout containers include the HBox container for laying out child components
horizontally, the VBox container for laying out child components vertically, and the Grid
container for laying out child components in rows and columns. Examples of navigator
containers include the TabNavigator container for creating tabbed panels, the Accordion
navigator container for creating collapsible panels, and the ViewStack navigator container for
laying out panels on top of each other.
The following example shows an application that contains a List control on the left side of the
user interface and a TabNavigator container on the right side:
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml">
<mx:Panel title="My Application" marginTop="10" marginBottom="10"
marginLeft="10" marginRight="10" >
<mx:HBox>
<!-- List with three items -->
<mx:List>
<mx:dataProvider>
<mx:Array>
<mx:String>Item 1</mx:String>
<mx:String>Item 2</mx:String>
<mx:String>Item 3</mx:String>
</mx:Array>
</mx:dataProvider>
</mx:List>
</mx:TabNavigator>
</mx:HBox>
</mx:Panel>
</mx:Application>
For more information about laying out user-interface components, see Chapter 1, “Using Flex
Components,” in Developing Flex Applications.
<mx:TextArea id="textarea1"/>
<mx:Button label="Submit" click="textarea1.text='Hello World';"/>
</mx:Panel>
</mx:Application>
The following figure shows the application rendered in a web browser window:
About MXML 27
The following example shows the code for a version of the application in which the event handler
is contained in an ActionScript function in an <mx:Script> tag:
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml">
<mx:Script>
<![CDATA[
function hello(){
textarea1.text="Hello World";
}
]]>
</mx:Script>
<mx:TextArea id="textarea1"/>
<mx:Button label="Submit" click="hello();"/>
</mx:Panel>
</mx:Application>
For more information about using ActionScript with MXML, see Chapter 16, “Working with
ActionScript in Flex,” in Developing Flex Applications.
</mx:Panel>
</mx:Application>
As an alternative to the curly braces ({ }) syntax, you can use the <mx:Binding> tag, in which you
specify the source and destination of a binding. For more information about data binding, see
Chapter 32, “Binding and Storing Data in Flex,” in Developing Flex Applications.
<!-- Bind the value of the ZIP code entered in the TextInput control
to the ZipCode parameter of the GetWeather operation. -->
<mx:operation name="GetWeather">
<mx:request>
<ZipCode>{zip.text}</ZipCode>
</mx:request>
</mx:operation>
</mx:WebService>
<mx:Panel title="My Application" marginTop="10" marginBottom="10"
marginLeft="10" marginRight="10" >
About MXML 29
<!-- Provide a ZIP code in a TextInput control. -->
<mx:TextInput id="zip" width="200" text="Zipcode please?"/>
<!-- Call the web service operation with a Button click. -->
<mx:Button width="60" label="Get Weather"
click='WeatherService.GetWeather.send()'/>
<!-- Display the location for the specified ZIP code. -->
<mx:Label text="Location:"/>
<mx:TextArea text="{WeatherService.GetWeather.result.Location}"/>
<!-- Display the current temperature for the specified ZIP code. -->
<mx:Label text="Temperature:"/>
<mx:TextArea text="{WeatherService.GetWeather.result.CurrentTemp}"/>
</mx:Panel>
</mx:Application>
The following figure shows the application rendered in a web browser window:
For more information about using data services, see Chapter 31, “Managing Data in Flex,” in
Developing Flex Applications.
</mx:Application>
The following figure shows the application rendered in a web browser window:
For more information about using data models, see Chapter 31, “Managing Data in Flex,” in
Developing Flex Applications.
<mx:Script>
<![CDATA[
var storedZipCode=123456789;
About MXML 31
]]>
</mx:Script>
<mx:Panel title="My Application" marginTop="10" marginBottom="10"
marginLeft="10" marginRight="10" >
<!-- Trigger the formatter while populating a string with data. -->
<mx:TextInput text="{ZipCodeDisplay.format(storedZipCode)}" />
</mx:Panel>
</mx:Application>
The following figure shows the application rendered in a web browser window:
For more information about formatter components, see Chapter 34, “Formatting Data,” in
Developing Flex Applications.
<mx:Style>
.myclass { color: Red } /* class selector */
Button { font-size: 18pt} /* type selector */
</mx:Style>
</mx:Panel>
</mx:Application>
For more information about using Cascading Style Sheets, see Chapter 20, “Using Styles and
Fonts,” in Developing Flex Applications.
Using effects
An effect is a change to a component that occurs over a brief period of time. Examples of effects
are fading, resizing, and moving a component. An effect is combined with a trigger, such as a
mouse click on a component, a component getting focus, or a component becoming visible, to
form a behavior. In MXML, you apply effects as properties of a control or container. Flex provides
a set of built-in effects with default properties.
The following example shows an application that contains a Button control with its
mouseOverEffect property set to the standard Zoom effect. The Zoom effect executes when the
user moves the mouse pointer over the Button control.
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml">
<mx:Panel title="My Application" marginTop="10" marginBottom="10"
marginLeft="10" marginRight="10" >
</mx:Panel>
</mx:Application>
You can use the <mx:Effect> tag to customize a standard effect or combine effects into a
composite effect. For more information about effects, see Chapter 21, “Using Behaviors,” in
Developing Flex Applications.
The following example shows a custom ComboBox control that is prepopulated with list items:
<?xml version="1.0"?>
<!-- MyComboBox.mxml -->
<mx:VBox xmlns:mx="http://www.macromedia.com/2003/mxml">
<mx:ComboBox >
About MXML 33
<mx:dataProvider>
<mx:Array>
<mx:String>Dogs</mx:String>
<mx:String>Cats</mx:String>
<mx:String>Mice</mx:String>
</mx:Array>
</mx:dataProvider>
</mx:ComboBox>
</mx:VBox>
The following example shows an application that uses the MyComboBox component as a custom
tag. The value * assigns the local namespace to the current directory.
<?xml version="1.0"?>
<!-- MyApplication.mxml -->
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml"
xmlns:local="*">
<mx:Panel title="My Application" marginTop="10" marginBottom="10"
marginLeft="10" marginRight="10" >
<local:MyComboBox/>
</mx:Panel>
</mx:Application>
The following figure shows the application rendered in a web browser window:
For more information about MXML components, see Chapter 15, “Building an Application with
Multiple MXML Files,” in Developing Flex Applications.
You can also define custom Flex components in ActionScript and the Flash MX 2004 authoring
tool. For more information, see Chapter 3, “Using ActionScript.”
<my:CustomBox/>
</mx:Panel>
</mx:Application>
The containers/boxes directory can be a subdirectory of the directory containing the application
file, or it can be a subdirectory of one of the ActionScript classpath directories assigned in the
flex-config.xml file. If copies of the same file exist in both places, Flex uses the file in the
application file directory. The prefix name is arbitrary, but it must be used as declared.
When using a component contained in a SWC file, the package name and the namespace must
match, even though the SWC file is in the same directory as the MXML file that uses it. For more
information about SWC components, see Developing Flex Applications.
Scalar properties
You usually specify the value of a scalar property as a property of a component tag, as the
following example shows:
<Label width="50" height="25" text="Hello World"/>
Note: A tag property cannot consist of the @ character followed by one or more characters.
Objects
When a component has a property that takes an object as its value, you can represent the property
in MXML using child tags, as the following example shows:
<mynamespace:MyComponent>
<mynamespace:nameOfProperty>
<objectType prop1="val1" prop2="val2"/>
</mynamespace:nameOfProperty>
</mynamespace:MyComponent>
The following example shows an ActionScript class that defines an Address object. This object is
used as a property of the PurchaseOrder component in the next example.
class example.Address
{
var name : String;
var street : String;
var city: String;
var state: String;
var zip: Number;
}
The following example shows an ActionScript class that defines a PurchaseOrder component that
has a property type of Address:
import example.Address;
class example.PurchaseOrder {
public var shippingAddress : Address;
public var quantity : Number;
...
}
In MXML, you define the PurchaseOrder component as the following example shows:
<e:PurchaseOrder quantity="3" xmlns:e="example">
<e:shippingAddress>
<e:Address name="Fred" street="123 Elm St."/>
</e:shippingAddress>
</e:PurchaseOrder>
If the property is explicitly typed as Object like the value property in the following example, you
can specify an anonymous object using the <mx:Object> tag.
class ObjectHolder {
var value : Object
}
The following example shows how you specify an anonymous object as the value of the value
property:
<mynamespace:ObjectHolder>
<mynamespace:value>
<mx:Object foo='bar' />
</mynamespace:value>
</mynamespace:ObjectHolder>
Arrays of objects
When a component has a property that takes an Array of objects as its value, you can represent
the property in MXML using child tags, as the following example shows:
<mynamespace:MyComponent>
<mynamespace:nameOfProperty>
<mx:Array>
<objectType prop1="val1" prop2="val2"/>
<objectType prop1="val1" prop2="val2"/>
<objectType prop1="val1" prop2="val2"/>
</mx:Array>
</mynamespace:nameOfProperty>
</mynamespace:MyComponent>
The component in the following example contains an Array of ListItem objects. Each ListItem
object has properties named label and data.
<mynamespace:MyComponent>
<mynamespace:dataProvider>
<mx:Array>
<mynamespace:ListItem label="One" data="1"/>
<mynamespace:ListItem label="Two" data="2"/>
</mx:Array>
</mynamespace:dataProvider>
</mynamespace:MyComponent>
Style properties
A style property of an MXML tag differs from other properties because it corresponds to an
ActionScript style property that you set in ActionScript using the setStyle(stylename,
value) method rather than object.property=value notation. The text and fontFamily
properties in the following example:
<mx:TextArea id="myText" text="hello world" fontFamily="Tahoma"/>
<mx:VBox>
<mx:TextInput id="myText" text="Hello World!" />
<mx:Button id="mybutton" label="Get Weather" click="writeToLog();"/>
</mx:VBox>
<mx:Script>
<![CDATA[
function writeToLog()
{
trace(myText.text);
}
]]>
</mx:Script>
</mx:Application>
Because each id value in an MXML file is unique, all objects in a file are part of the same flat
namespace. You do not qualify an object by referencing its parent with dot notation, as in
myVBox.myText.text.
XML standards
You write Flex applications in XML documents. XML documents use tags to define pieces of
structured information and the relationships between them. In MXML, the <mx:Application>
tag defines the root of an application. You define the parts of the application in child tags of the
<mx:Application> tag. Examples of MXML tags include container tags, such as <mx:VBox>,
which define rectangular regions of the user interface, and control tags, such as <mx:TextInput>,
which define typical user interface controls.
Java standards
Flex provides MXML tags for interacting with server-side Java objects, including plain old Java
objects (POJOs), JavaBeans, and Enterprise JavaBeans (EJBs).
HTTP standards
Flex provides MXML tags for making standard HTTP GET and POST requests, and working
with data returned in HTTP responses.
Graphics standards
Flex provides MXML tags for using JPEG, GIF, and PNG images in an application. Flex also
provides tags for importing Macromedia SWF files and Scalable Vector Graphics (SVG) files into
an application.
Macromedia Flex developers can use ActionScript to extend the functionality of their Flex
applications. ActionScript provides flow control and object manipulation features that are not
available in strict MXML. This chapter introduces ActionScript and explains how to use
ActionScript in an MXML application.
For more information on ActionScript, see Flex ActionScript Language Reference.
Contents
About ActionScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Using ActionScript in Flex applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Creating ActionScript components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Techniques for separating ActionScript from MXML. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
About ActionScript
ActionScript is the programming language for Macromedia Flash Player. ActionScript includes
built-in objects and functions, and lets you create your own objects and functions.
Note: The ActionScript programming language in Flash MX 2004 and Flex follows the ECMA 4
standard proposed by the Mozilla project.
ActionScript is similar to the core JavaScript programming language. You don’t need to know
JavaScript to use ActionScript; however, if you know JavaScript, ActionScript will appear familiar
to you. The following are some of the many resources available that provide more information
about general programming concepts and the JavaScript language:
• The European Computers Manufacturers Association (ECMA) document ECMA-262 is
derived from JavaScript and serves as the international standard for the JavaScript language.
• ActionScript is based on the ECMA-262 specification, which is available from www.ecma-
international.org. ECMA-262 is also known as ECMAScript. ActionScript is primarily based
on Edition 3 of this standard, which is also referred to commonly as ECMA 3 (shorthand for
ECMA-262 Edition 3).
43
• Netscape DevEdge Online has a JavaScript Developer Central site
(http://developer.netscape.com/tech/javascript/index.html) that contains documentation and
articles about ActionScript.
• Core JavaScript Guide.
For more information about using ActionScript in Flex, see Flex ActionScript Language Reference,
which is included in the Flex documentation set.
ActionScript compilation
The logic of your Flex application can consist of ActionScript classes, MXML files, SWF files, and
external components in the form of SWC components, MXML files, or ActionScript files. The
end result of all the input files is a SWF file that is downloaded by Flash Player and played on the
client’s machine.
You can use ActionScript in code fragments within your MXML files. The Flex compiler
transforms the main MXML file and its child files into a single ActionScript class. As a result, you
cannot define classes or use statements outside of functions in MXML files and included
ActionScript files.
You can reference imported ActionScript classes from your MXML application files, and those
classes are added to the final SWF file.
When the transformation to an ActionScript file is complete, Flex links all the ActionScript
components and includes those classes in the final SWF file.
About ActionScript 45
The following figure shows the source files used to generate a SWF file that your J2EE server
sends to the client:
Source Files
The top-level
MXML file
includes
<mx:script> blocks,
imported Compiler/Linker
ActionScript files, J2EE Server
and components
defined by Client
ActionScript files
or MXML files.
ActionScript
classes and
packages
referenced in the
application files
are included in the
final SWF.
To access that component’s methods or properties, use dot syntax. The following example gets the
value of the TextArea named ta1:
var str:String = ta1.text;
You can refer to the current enclosing document or current object using the this keyword. For
more information, see Chapter 16, “Working with ActionScript in Flex,” in Developing Flex
Applications.
<mx:Script>
<![CDATA[
var z:Number;
function doSomething() {
z = z + 1; // This must be in a function.
}
]]>
</mx:Script>
...
</mx:Application>
The following example imports the contents of the IncludedFile.as file. This file is located in the
same directory as the MXML file.
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml">
<mx:TextArea id="taMain"/>
</mx:Application>
The source attribute of the <mx:Script> tag supports both relative and absolute paths. For more
information, see “Referring to external files” on page 51.
You cannot use the source attribute and wrap ActionScript code inside the same <mx:Script>
tag. To include a file and write ActionScript in the MXML file, use two <mx:Script> tags.
Note: The #include directive is a special kind of ActionScript statement. Do not end the line with a
semicolon. If you do, you get a “Malformed #include directive” error message. In addition, you must
surround the target file with quotation marks.
You can only specify a single file for each #include directive, but you can use any number of
#include directives. You can next #include directives; files with #include directives can include
files that have #include directives.
The #include directive supports only relative paths. For more information, see “Referring to
external files” on page 51.
You can only use the #include where multiple statements are allowed. For example, the following
is not allowed:
if (expr)
#include "foo.as" // First statement is guarded by IF, but the rest are not.
...
The use of curly braces ({ }) allows multiple statements because you can add multiple statements
inside the braces.
Macromedia recommends against using the #include directive if you use a large number of
included ActionScript files. You should try to break the code into separate class files where
appropriate and store them in logical package structures.
Flex searches the ActionScript classpath for imported classes and packages. Flex does not search the
ActionScript classpath for files that are included using the #include directive or the source
attribute of the <mx:Script> tag.
In your ActionScript code, instead of referring to the class with its fully qualified package name
(MyPackage.Util.MyClass), you can refer to it as MyClass, as the following example shows:
import MyPackage.Util.MyClass;
function createClass() {
tObj = new MyClass;
}
Note: All statements other than import and variable declarations must be inside functions in an
<mx:Script> tag.
Flex searches the ActionScript classpath for imported files and packages.
If you import a class but do not use it in your application, the class is not included in the resulting
SWF file’s bytecode. As a result, importing an entire package with a wildcard does not create an
unnecessarily large SWF file.
For more information on the <compiler> settings, see Chapter 39, “Administering Flex,” in
Developing Flex Applications.
In this example, you write your MyButton control to the MyButton.as file, and you store the file
in the myControls subdirectory of the root directory of your Flex application. The fully qualified
class name of your component reflects its location. In this example, the component’s fully
qualified class name is myControls.MyButton.
You can reference your custom Button control from a Flex application file, such as MyApp.mxml,
as the following example shows:
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml"
xmlns:cmp="myControls.*" >
</mx:Application>
In this example, you first define the cmp namespace that defines the location of your custom
component in the application’s directory structure. You then reference the component as an
MXML tag using the namespace prefix.
Typically, you put custom ActionScript components in directories that are in the ActionScript
classpath. These include your application’s root directory, the flex_app_root/WEB-
INF/flex/user_classes directory, or any directory you specify in the <actionscript-classpath>
tag in the flex-config.xml file. For more information, see Chapter 18, “Creating ActionScript
Components” in Developing Flex Applications.
There are several ways to separate MXML and ActionScript in this simple application that calls a
single function:
• One MXML document (ActionScript event handling logic in MXML tag)
• One MXML document (function call in MXML tag event)
• One MXML document and one ActionScript file
• One codeless MXML document and one ActionScript component
The following sections describe these methods.
<mx:Script>
<![CDATA[
function calculate() {
celsius.text=(farenheit.text-32)/1.8;
}
]]>
</mx:Script>
<mx:Panel title="My Application" marginTop="10" marginBottom="10"
marginLeft="10" marginRight="10" >
<mx:HBox>
<mx:Label text="Temperature in Farenheit:"/>
<mx:TextInput id="farenheit" width="120"/>
<mx:Button label="Convert" click="calculate();" />
<mx:Label text="Temperature in Celsius:"/>
<mx:Label id="celsius" width="120" fontSize="48"/>
</mx:HBox>
</mx:Panel>
</mx:Application>
function click(event) {
view.celsius.text=(view.farenheit.text-32)/1.8;
}
TempConverter is an MXMLObject and the compiler calls the initialized() method on all
instances of MXMLObject.
This chapter describes the Macromedia Flex development process. It describes coding practices,
multitier applications, and application architecture. It also includes information about MXML
URL parameters, the Flex development environment, and Flex development tools.
When you develop a Flex application, you use the same iterative process that you use for other
types of web application files, such as HTML, JavaServer Pages (JSP), Microsoft Active Server
Pages (ASP), and Macromedia ColdFusion Markup Language (CFML). Unlike a set of static
HTML pages or HTML templates created using JSP, ASP, or CFML, the files in a Flex
application are compiled into a single binary SWF file. JSP, ASP, and ColdFusion are templating
systems in which application processing occurs on the server and data is dynamically added to an
HTML template and delivered to the client in an HTML page.
Contents
About the Flex coding process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Working with a multitier application model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Improving application start-up time and performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Architecting an application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Summary of Flex application features. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
57
About the Flex coding process
Creating a useful Flex application is as easy as opening your favorite text editor, typing some XML
tags, saving the file, opening the file’s URL in a web browser, and then repeating the process.
When you develop a Flex application, you use the same iterative process that you use for other
types of web application files, such as HTML, JSP, ASP, and CFML.
Unlike a set of static HTML pages or HTML templates created using JSP, ASP, or CFML, the
files in a Flex application are compiled into a single binary SWF file. Another major difference
between a Flex application and a JSP, ASP, or ColdFusion application is that application logic
resides in the client-side SWF file. JSP, ASP, and ColdFusion are templating systems in which
application processing occurs on the server and data is dynamically added to an HTML template
and delivered to the client in an HTML page. For more information, see “Moving to Flex from
HTML” on page 58 and “Moving to Flex from an HTML templating environment” on page 59.
Because MXML files are ordinary XML files, you have a wide choice of development
environments. You can develop in a simple text editor, a dedicated XML editor, or an integrated
development environment (IDE) that supports text editing. Flex also provides tools for code
debugging and performance profiling; for more information, see “Using Flex development tools”
on page 60.
Directory Description
web_app Contains the WEB-INF directory and all files that must be
(root directory or WAR root) accessible by the user’s web browser, such as MXML files, JSPs,
HTML pages, Cascading Style Sheets, images, and JavaScript
files. You can place these files directly in the web application root
directory or in arbitrary subdirectories that do not use the
reserved name WEB-INF.
/WEB-INF/flex/system-classes Contains ActionScript standard library stub classes that Flex use
internally for type-checking.
/WEB-INF/flex/frameworks Contains the SWC component file, builtin.swc, that contains the
Flex application framework files.
If <production-mode> is set to true, Flex disables all debugging and profiling features. Flex also
ignores query string parameter overrides such as ?debug=true and ?asprofile=true when
<production-mode> is enabled.
Tool Description
Macromedia Flash Debug Player Flash Debug Player is a Flash Player that reports runtime errors,
including errors for undeclared variables, uncaught runtime
exceptions, and operating-system–specific networking errors.
You can view errors in a trace window and send errors to a text
file.
When you run Flash Debug Player, you specify the subsystems
to monitor and request the compiled SWF file, through the Flex
server.
To use the Flash Debug Player, you must disable production
mode if it is enabled and edit debugging settings in the flex-
config.xml file. For more information, see Chapter 36,
“Debugging Flex Applications,” in Developing Flex Applications.
command-line debugger The Flex ActionScript debugger lets you step into and debug
ActionScript files used by your Flex applications.
To use the command-line debugger, you must disable
production mode if it is enabled and edit debugging settings in
the flex-config.xml file. For more information, see Chapter 36,
“Debugging Flex Applications,” in Developing Flex Applications.
compc The compc utility generates a SWC file from MXML component
source files and/or ActionScript component source files.
You can get additional information about Flex applications during the development process using
a set of query string parameters. You append these to the end of the request string, as the
following example shows:
http://localhost:8700/flex/MyApplication.mxml?debug=true
To use most of these query string parameters, you must disable production mode in the flex-
config.xml file by setting the value of the <production-mode> tag to false.
For more information about application configuration, see Chapter 39, “Administering Flex,” in
Developing Flex Applications.
Feature Description
Data service Accesses data from a remote location, and sends new or modified data to a remote
location. A typical way to send data to a data service is by sending data stored in a
user interface control to the data service. For example, a user fills in the fields of a
form and clicks a button to submit the form to a service. The user input data
becomes part of the service request. Data that the service returns is stored in the
service’s result property.
Data model Stores data in fields that represent each part of a specific data set. For example, a
person model stores data such as a person’s name, age, and phone number. Data
services contain data models to store request data and result data.
Data validator Validates data in data model fields. For example, you can use a data validator to
validate the data in a form before submitting it to a data service.
Data formatter Converts raw data to a formatted string. Formatters are triggered just before data is
displayed in a text field. They can save you time by automating data formatting
tasks, and by letting you easily change the formatting of fields within your
applications.
Data binding Copies data stored in one object to another object. For example, you can bind data
from the fields of a user input form to fields in a data service request. To display
data that a data service returns, you can bind fields from the data service result to
user interface controls.
<mx:Script>
<![CDATA[
function myFooResultHandler(foo) {
myService.getBarWithFooInput(foo);
}
]]>
</mx:Script>
...
For more information about event handling, see Chapter 16, “Working with ActionScript in
Flex,” in Developing Flex Applications.
<mx:Script>
<![CDATA[
function initMyApp(){
var myMenu=new ContextMenu();
myMenu.builtInItems.zoom = true;
document.menu=myMenu;
}
]]>
</mx:Script>
...
</mx:Application>
Nesting containers
Application start-up time slows commensurate with the complexity of the layout of your Flex
application. Excessive levels of nested containers can contribute to this complexity, and should be
avoided if you can accomplish the same layout with fewer levels of nesting. Try using margin, gap,
and alignment properties and the Spacer control, to achieve the desired layout.
When you have a working skeleton of your application, consider the containers that you are using
to determine if you could use alternate containers with less nesting. For example, consider the
following layout:
<mx:Grid>
mx:GridRow>
<mx:GridItem>
<mx:Button label="Visa"/>
</mx:GridItem>
<mx:GridItem>
<mx:Button label="MasterCard"/>
</mx:GridItem>
<mx:GridItem>
<mx:Button label="Diner's Club"/>
</mx:GridItem>
<mx:GridItem>
<mx:Button label="AmEx"/>
</mx:GridItem>
</mx:GridRow>
</mx:Grid>
Architecting an application
After you determine the purpose and general features of an application, you need to consider the
following topics:
• What the object model should be
• What types of Flex components you should use to represent the object model
• What type of messaging strategy you should use to pass data between objects
This section uses a fairly simple e-commerce application called the Flex Store to illustrate general
ways to approach object models, component types, and messaging strategies. The application
demonstrates clear separation of model and view objects, and loose coupling of components. It
uses a combination of standard Flex components, MXML components, SWC components, and
ActionScript components.
The purpose of the Flex Store application is to display a catalog of items that the user can add to a
shopping cart, and then order. The following list includes some of the things a user can do with
the application:
• Click a product thumbnail image to display a larger image and description.
• Select a price range for products.
• Add items to the shopping cart.
• Place orders in a checkout form.
Architecting an application 71
Flex Store object model figure
The following figure shows the object model for the Flex Store application and identifies each
object as part of the model, view, or controller:
FlexStore
ShoppingCart View catalog
Model Model
CartView ProductDetail
View View
FlexStore
The FlexStore object is the top-level object in the Flex Store application. It contains the rest of the
application objects, including the standard Flex objects and custom objects. This object is a view
object because it is the top-level object in the user interface, but it also contains the rest of the
application.
catalog
The catalog object is a nonvisual object that contains data about the products available for
purchase. This object is a model object because its primary purpose is to represent products.
ShoppingCart
The ShoppingCart object is a nonvisual object that stores items a user selects for purchase. This
object is a model object because its primary purpose is to represent a set of items that the user
selects for purchase. It also keeps track of the number of items selected for purchase and the total
cost of the items.
CartView
The CartView object is a visual object that displays the current ShoppingCart items in the user
interface. This object must get the current data from the ShoppingCart object and display it
appropriately in the user interface; this requirement is described in “Determining component
types and a messaging strategy” on page 73.
ProductThumbnail
The ProductThumbnail object is a visual object that displays a thumbnail image, name, and price
for each product in the catalog object.
ProductDetail
The ProductDetail object is a visual object that displays a large image, name, price, description,
and quantity selector for a selected ProductThumbnail object or a selected item in the
ShoppingCart object. This object also lets the user add a selected item to the ShoppingCart
object.
This object must get the index of the selected ProductThumbnail or ShoppingCart item and
display the corresponding product data appropriately in the user interface; this requirement is
described in “Determining component types and a messaging strategy” on page 73.
Checkout
The Checkout object is a visual object that contains a form for placing an order based on the
current items in a ShoppingCart object. This object must get the current data from the
ShoppingCart object and display it appropriately in the user interface; this requirement is
described in “Determining component types and a messaging strategy” on page 73.
Architecting an application 73
Flex Store component figure
The following figure shows the component type of each object in the object model of the Flex
Store application. The Data Object boxes show which objects contain data object properties.
FlexStore
(MXML Application
ShoppingCart object) catalog
(ActionScript component) View <mx:Model> tag
Model Model
CartView ProductDetail
(MXML component) (MXML component)
View View
cartData dataObject
variable of type variable
ShoppingCart Data Object
Data Object
FlexStore
The FlexStore object, the top-level application object, is an MXML file. It contains standard
MXML tags that declare most of the application, and custom MXML tags that declare custom
objects. For the custom tags, this file declares them to use the default namespace.
The FlexStore object also contains some application-level ActionScript code that helps glue the
application together, and Cascading Style Sheet (CSS) definitions. The ActionScript code and
CSS definitions are contained in external files to promote modularity and reusability.
catalog
The catalog object is declared in an <mx:Model> tag in the FlexStore.mxml file because it is a
simple object with the sole function of data storage, and it requires no data typing. Compare this
with the ShoppingCart object, which is also in the model tier, but stores and manipulates data.
The data for the catalog object is defined in an external XML file, catalog.xml, which is accessed
using a named web service and the following <mx:WebService> data service tag:
<mx:WebService id="catalogWS" serviceName="FlexStoreCatalogWS">
<mx:operation
name="getList"
result="selectedItem=catalogWS.getList.result[0]"
/>
</mx:WebService>
Data could just as easily be accessed using an HTTP service or a server-side Java object that
retrieves data from a remote data source.
ShoppingCart
The ShoppingCart object is defined in an ActionScript class because it performs data storage and
data manipulation. An ActionScript class is the appropriate component type because it can
contain both properties and methods.
The ShoppingCart class contains the following properties:
• var items
• var total
CartView
The CartView object is defined in an MXML component because it is a visual object that you can
create using MXML tags and a small amount of ActionScript code. The top-level tag of the
CartView component is a Panel container.
The CartView component uses a <mx:Script> tag to include the ActionScript file
CartView_script.as. That file contains the following property definitions:
• var selectedItem
• var dataObject
Architecting an application 75
The CartView_script.as file also contains the following method definitions:
• dataGridChange(selectedItem)
• removeItem()
ProductThumbnail
The ProductThumbnail object defines a custom cell renderer used by the ThumbnailView object.
You use a cell renderer to manipulate and display custom cell content for each of row of a list-
based component. In this example, you use ProductThumbnail to define an image and two labels
in each cell of a TileList control.
The ProductThumbnail component contains the following properties defined in an
<mx:Script> tag:
• var _filteredOut
• var dataObject
The following example from the ThumbnailView.mxml file shows how the dataObject property
is set to the catalog data for the currentItem in the catalog object, and the cell renderer is set to
the ProductThumbnail object:
...
<mx:Script>
<![CDATA[
var dataObject;
var selectedItem;
]]>
</mx:Script>
ThumbnailView
The ThumbnailView object is defined in an MXML component because it is a visual object that
you can create using MXML tags and a small amount of ActionScript code. The top-level tag of
the ThumbnailView component is a VBox container.
The component is declared in the FlexStore.mxml file using a <ThumbnailView> tag.
The component contains the following properties defined in an <mx:Script> tag:
• var selectedItem
• var dataObject
• var min
• var max
ProductDetail
The ProductDetail object is defined in an MXML component because it is a visual object that
you can create using MXML tags and a small amount of ActionScript code. The top-level tag of
the ProductDetail component is a VBox container.
Architecting an application 77
The component contains the following property definitions in an <mx:Script> tag:
• var shoppingCart
• var dataObject
Checkout
The Checkout object is defined in an MXML component because it is a visual object that you can
create using MXML tags and a small amount of ActionScript code. The top-level tag of the
Checkout component is a VBox container.
The component uses an <mx:Script> tag to include the ActionScript file Checkout_script.as.
The Checkout_script.as file contains the following property definitions:
• var cartData
• var months
• var years
• var shippingCost
The Checkout component contains an <mx:Form> tag for entering order information, and an
<mx:Model id="order"> tag for storing order information. It also contains data formatter tags
and data validator tags.
The following example from the FlexStore.mxml file shows how the shoppingCart property is
set to the cart ShoppingCart object. Notice that you reference the custom ShoppingCart and
Checkout objects using the default namespace.
...
<ShoppingCart id="cart"/>
...
<Checkout id="checkoutView" cartData="{cart}"/>
Feature Description
User interface controls Controls are user-interface components, such as Button, TextArea, and
ComboBox controls. You use MXML tags to add controls to an
application.
For more information, see Chapter 2, “Using Controls,” in Developing
Flex Applications.
User interface containers Containers are user-interface components that let you control the
layout characteristics of the user-interface components that they
contain. You can use containers to control child sizing and positioning,
or to control navigation among multiple child containers. You use
MXML tags to add containers to an application.
For more information, see Chapter 4, “Introducing Containers,” in
Developing Flex Applications.
MXML components MXML components are Flex components written in MXML files. They
provide an easy way to extend an existing Flex component and
encapsulate the appearance and behavior of a component in a custom
MXML tag. You use MXML tags to add MXML components to an
application.
For more information, see Chapter 15, “Building an Application with
Multiple MXML Files,” in Developing Flex Applications.
SWC components SWC components are created in the Flash MX authoring environment
and exported in SWC files for use in Flex applications. They are a good
choice for complex visual objects. You use MXML tags to add SWC
components to an application.
For more information, see Developing Flex Applications.
Data binding The data binding feature provides a simple syntax for automatically
copying the value of a property of one client-side object to a property of
another object at runtime.
For more information, see Chapter 32, “Binding and Storing Data in
Flex,” in Developing Flex Applications.
Data models Data models let you store application-specific data. They are used for
data validation and can contain client-side business logic. You can
define data models in ActionScript classes or MXML tags.
For more information, see Chapter 32, “Binding and Storing Data in
Flex,” in Developing Flex Applications.
Data services Data service objects let you interact with server-side data sources. You
can work with data sources that are accessible using SOAP-compliant
web services, Java objects, or HTTP GET or POST requests.
For more information, see Chapter 31, “Managing Data in Flex,” in
Developing Flex Applications.
Data validation Data validators help you ensure that the values in the fields of a data
model meet certain criteria. For example, you can use a validator to
check whether a user entered a valid ZIP code value in a TextInput
control.
For more information, see Chapter 33, “Validating Data in Flex,” in
Developing Flex Applications.
Data formatting Data formatters let you format data into strings before displaying it in
the user interface. For example, you can use a formatter to display a
phone number in a specific format.
For more information, see Chapter 34, “Formatting Data,” in
Developing Flex Applications.
Cursor management Cursor management lets you control the cursor image within a Flex
application. You can use cursor management to provide visual
feedback to users to indicate when to wait for processing to complete,
to indicate allowable actions, or to provide other types of feedback.
For more information, see Chapter 24, “Using the Cursor Manager,” in
Developing Flex Applications.
History management History management lets users navigate through a Flex application
using the web browser’s Back and Forward navigation commands. It
also lets users return to the previous location in an application if the
browser accidentally navigates away from that location.
For more information, see Chapter 26, “Using the History Manager,” in
Developing Flex Applications.
Drag-and-drop Drag-and-drop management lets you move data from one place in a
management Flex application to another. This feature is especially useful in a visual
application where your data can be items in a list, images, or Flex
components.
For more information, see Chapter 22, “Using the Drag and Drop
Manager,” in Developing Flex Applications.
ToolTips ToolTips let you provide helpful information to application users. When
a user moves their mouse over a graphical component, a toolTip pops
up and displays its text.
For more information, see Chapter 23, “Using ToolTips,” in Developing
Flex Applications.
Styles, fonts, and themes Styles, fonts, and themes help you define the overall appearance of
applications. You can use them to change the appearance of a single
component, or apply them across all components.
For more information, see Chapter 20, “Using Styles and Fonts,” in
Developing Flex Applications.
JSP tag library You can use the Flex JSP tag library to add MXML code to your JSPs
or create custom HTML wrappers for your Flex applications.
For more information, see Chapter 38, “Using the Flex JSP Tag
Library,” in Developing Flex Applications.
ActionScript scripting ActionScript lets you perform actions with the components that are
represented by MXML tags. You use ActionScript in your Flex
applications to do the following:
• Handle events
• Define custom functions and methods
• Call ActionScript functions
• Work with components after they are instantiated
For more information, see Chapter 16, “Working with ActionScript in
Flex,” in Developing Flex Applications.
Debugging Flex includes support for debugging and warning messages, an error-
reporting mechanism, and a command-line ActionScript debugger to
assist you in debugging your application.
For more information, see Chapter 36, “Debugging Flex Applications,”
in Developing Flex Applications.
ActionScript profiling The ActionScript Profiler helps you identify performance problems in
applications. It can show you where too many calls to a particular
method might be occurring or where an object’s instantiation might be
taking too long.
For more information, see Chapter 37, “Profiling ActionScript,” in
Developing Flex Applications.
Administration Flex includes several configuration files to administer the behavior of
your applications. Settings specific to the Flex application are defined
in the flex_app_root/WEB-INF/flex/flex-config.xml file. Web application
settings are located in the flex_app_root/WEB-INF/web.xml file.
You can use the mxmlc tool included with Flex to compile your MXML
files into SWF files without requesting them from a browser or Flash
Player. To use mxmlc, you must have a Java runtime environment
(JRE) in your system path.
For more information, see Chapter 39, “Administering Flex,” in
Developing Flex Applications.
A C
ActionScript CartView object 72, 75
about 43 Cascading Style Sheets (CSS) 32
class import 51 catalog object 72, 74
classpath 52 characters, special 46
compared to ECMAScript 44 Checkout object 78
compared to JavaScript 44 classes classpath 52
compiling 45 classpath, in ActionScript and SWC files 52
creating components 53 ColdFusion, moving to Flex 59
custom components 53 command-line debugger 61
defining components 53 compc 61
examples 54 compiler setting 52
identifiers 47 compiler tags 40
importing classes 51 compiling, ActionScript classes 52
importing files 48 components
in MXML 44, 47 about 12
including and importing 48 creating in ActionScript 53
including files 48 example 12
package import 51 in MXML 36
packages 45 setting properties of 36
script blocks 48 sizing 67
Script tag 48 containers
special characters 46 about 12
using 17, 46 example 12
ActionScript Profiler 61 nesting 68
Application container, about 25 performance tips 68
applications controls 12
appearance 67 CSS, and MXML 32
architecture of 70 custom components
developing 57 about 33, 53
ASP, and Flex coding 58 example 53
in ActionScript 53
B in MXML 33
behaviors types 54
about 67
performance tuning 69
83
D data binding 13
data binding data models 13
about 13 development environment 60
MXML syntax 28 development process 14
data formatters 31 development tools 60
data models features 19
about 13 Flash Debug Player and 10
in MXML 30 Flash Player and 10
using 12 J2EE servers and 9
data services MVC architecture 13
and MXML components 64 MXML 16
in MXML 29 n-tier model and 8
development environment, Flex 60 profiling 10
programming model 15
E runtime services 15
ECMAScript 44 standards supported 10
effects URL to 14
about 33 Flex applications
changing components 33 appearance 67
performance tuning 69 architecting 57
using 33 behaviors 67
event handlers coding process 58
definition 27 data services 64
error handling 66 event handlers 65
in MXML 27 example 70
using 27, 66 laying out interface 63
event model, standards in MXML 42 organizing data 63
performance tuning 67
F separating data model from view 64
faceless components, definition 54 sizing components 67
features, summary 79 skins 67
file naming, MXML 36 styles 67
Flash Debug Player Flex development environment 60
about 10 Flex development tools
performance 70 about 60
using 10 ActionScript Profiler 61
Flash Player 10 command-line debugger 61
Flash, moving to Flex 59 compc 61
Flex Flash Debug Player 61
about 7 mxmlc 61
ActionScript and 17 Flex Store
application examples 11 application 71
application framework 15 component diagram 74
application model 12 object model diagram 72
benefits 9 FlexStore object 72, 74
class library 15
components 12
containers 12
controls 12
84 Index
G style property of tag 39
graphics, standards in MXML 42 syntax 35
tag syntax 41
H tags 36
HTML, moving to Flex 58 URLs 24
HTTP services, standards in MXML 42 using 16
using ActionScript with 27
I XML data 39
id property 47 XML namespaces 34
identifiers 40 MXML components
include directive 50 about 33
including ActionScript 48 data service components 64
MXML syntax
J about 35
J2EE servers 9 array properties 37
Java, standards in MXML 42 compiler tags 40
JavaScript, compared to ECMAScript 43 CSS 32
data binding 28
L data formatters 31
data models 30
languages
data services 29
ActionScript 43
effects 33
MXML 23
file naming 36
layout containers, performance tuning 68
identifiers 40
local namespaces 34
objects 37
requirements 41
M
scalar properties 36
model-view-controller architecture 71
Script tag 48
MVC architecture
setting component properties 36
about 71
styles 39
Flex and 13
tags 36, 41
MXML
XML properties 39
about 23
mxmlc 61
ActionScript and 44, 47
ActionScript classes and 15
N
controlling user interaction 26
namespaces 34
CSS and 32
data binding 28
O
data formatting 31
data models 30 object model, developing 71
data services 29
effects 33 P
event handlers 27 performance tuning
example 24 containers 68
Hello World example 24 effects 69
moving from Flash 23 Flash Debug Player 70
Script tag 48 initial layout 68
separating from ActionScript 54 runtime 68
simple applications 24
standards and 41
Index 85
Product Thumbnail object 72, 73 Z
ProductDetail object 77 zooming, applications 67
ProductThumbnail object 76
properties, setting in MXML 36
Q
query parameters 62
R
relative paths 51
S
Script tag
about 47
class import 51
external file reference 51
in MXML 48
include directive 50
including ActionScript files 48
package import 51
source attribute of 49
using 49
with ActionScript 48
ShoppingCart object 72, 75
skins 67
standards, in MXML 41
styles
about 67
in MXML 39
SWC files, classpath 52
T
tags
compiler 40
in MXML 36
ThumbnailView object 73, 77
tools, development 60
U
user interface tags 26
user_classes, directory 52
W
web services, standards in MXML 42
X
XML data 39
XML, namespaces 34
86 Index