Tải bản đầy đủ (.pdf) (764 trang)

Flex 4 Cookbook docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (8.09 MB, 764 trang )

www.it-ebooks.info
www.it-ebooks.info
Flex 4 Cookbook
www.it-ebooks.info
www.it-ebooks.info
Flex 4 Cookbook
Joshua Noble, Todd Anderson, Garth Braithwaite,
Marco Casario, and Rich Tretola
Beijing

Cambridge

Farnham

Köln

Sebastopol

Taipei

Tokyo
www.it-ebooks.info
Flex 4 Cookbook
by Joshua Noble, Todd Anderson, Garth Braithwaite, Marco Casario, and Rich Tretola
Copyright © 2010 O’Reilly Media, Inc. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly
books
may be purchased for educational, business, or sales promotional use. Online editions
are also available for most titles (). For more information, contact our


corporate/institutional sales department: 800-998-9938 or
Editor: Mary E. Treseler
Development Editor: Linda LaFlamme
Production Editor: Kristen Borg
Copyeditor: Rachel Head
Proofreader: Kiel Van Horn
Indexer: Ellen Troutman Zaig
Cover Designer: Karen Montgomery
Interior Designer: David Futato
Illustrator: Robert Romano
Printing History:
May 2010:
First Edition.
Nutshell
Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of
O’Reilly Media,
Inc. Flex 4 Cookbook, the image of a Kuhl’s flying gecko, and related trade dress are
trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a
trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and authors assume
no responsibility for errors or omissions, or for damages resulting from the use of the information con-
tained herein.
TM
This book uses RepKover™, a durable and flexible lay-flat binding.
ISBN: 978-0-596-80561-6
[M]
1273589930
www.it-ebooks.info

Table of Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
1.
Flex and ActionScript Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 Create a Flex Project in Flash Builder 2
1.2 Create a Flex Library Project in Flash Builder 8
1.3 Set Compiler Options in Flash Builder 9
1.4 Compile a Flex Project Without Flash Builder 12
1.5 Add an Event Listener in MXML 14
1.6 Create Typed Vectors 16
1.7 Use Event Bubbling 17
1.8 Use Custom Events and Dispatch Data with Events 19
1.9 Listen for a Keyboard Event 20
1.10 Define Optional Parameters for Methods 21
1.11 Define and Implement an Interface 22
1.12 Access the Parent of a Flex Component 24
2. Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.1 Position Children Within a Container 28
2.2 Dynamically Add and Remove Children 30
2.3 Reorder Child Elements of a Container 33
2.4 Display Children Using Data Items 35
2.5 Use a Custom Item Renderer in a DataGroup 38
2.6 Use Multiple Item Renderers in a DataGroup 41
2.7 Enable Scrolling in a Container 43
2.8 Scale Children of a Container 48
2.9 Apply Skins to a Container 50
2.10 Set the Background Image of a BorderContainer 55
2.11 Use a Control Bar 57
2.12 Modify Layout of Content Elements in a Panel 59
2.13 Track Mouse Position Within a Container 62

2.14 Drag and Drop Between Visual Containers 64
v
www.it-ebooks.info
2.15 Drag and Drop Between Data Containers 68
2.16 Add a Spark Layout Container to a MX Navigation Container 72
2.17 Create a Spark-Based ViewStack 74
3. Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
3.1 Position Children Linearly 82
3.2 Switch Layout Management at Runtime 83
3.3 Align and Size Children Within a Layout 85
3.4 Lay Out Children Using Rows and Columns 88
3.5 Size Children Uniformly 91
3.6 Lazily Create and Recycle Children 92
3.7 Create a Custom Layout 95
3.8 Measure and Alter the Container Size 97
3.9 Dynamically Change the Child Depth in the Layout 100
3.10 Use Matrix3D to Apply Transformations Within a Layout 102
3.11 Use TransformOffsets to Apply Transformations Within a Layout 104
3.12 Create a Custom 3D Layout 107
3.13 Programmatically Scroll Within a Layout 110
3.14 Determine the Visibility of Elements in a Sequence-Based Layout 114
4. Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
4.1
Size and Position a Graphic Element 119
4.2 Use Path to Draw a Shape with Stroke and Fill 120
4.3 Display Text in a Graphic Element 124
4.4 Display Bitmap Data in a Graphic Element 126
4.5 Display Gradient Text 129
4.6 Apply Bitmap Data to a Graphic Element as a Mask 130
4.7 Create a Custom Shape Element 132

4.8 Create a Custom Standalone Graphic Component 136
4.9 Define and Reuse Graphic Symbols 138
5. Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
5.1
Handle a Button’s Click Event 141
5.2 Create a Button Bar 144
5.3 Load a External SWF 146
5.4 Use a Calendar Date Input 147
5.5 Create Event Handlers for Menu-Based Controls 150
5.6 Display an Alert in an Application 151
5.7 Display a Custom Pop Up in a Custom Component 153
5.8 Detect a Mouse Click Outside a Pop Up to Close It 155
5.9 Using s:Scroller to Create a Scrollable Container 156
5.10 Handle focusIn and focusOut Events 157
5.11 Open a DropDownList with a Keyboard Shortcut 158
vi | Table of Contents
www.it-ebooks.info
5.12 Grouping Radio Buttons 160
5.13 Submit a Flex Form to a Server-Side Script 162
6. Skinning and Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
6.1 Create a Skin for s:Button 165
6.2 Apply a Repeating Background Image to an Application 168
6.3 Create a Skin for s:ButtonBar and s:ButtonBarButton 169
6.4 Skin an s:DropDownList 171
6.5 Skin a Spark Container 174
6.6 Change the Appearance of Components Using Styles 176
6.7 Apply Skins and Properties to Spark and MX Components with CSS 177
6.8 Create a Button Component with an Icon 179
6.9 Add Custom Style Properties 181
6.10 Partially Embed Fonts with CSS 183

7. Text and TextFlows . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
7.1 Create a TextFlow Object 186
7.2 Generate a TextFlow Object from Another Source 187
7.3 Create Links in a TextFlow 189
7.4 Add Graphic Elements to a TextFlow 190
7.5 Bind a Value to a s:TextInput Control 192
7.6 Create a Custom Selection Style 193
7.7 Style Links Within a TextFlow 195
7.8 Locate Elements Within a TextFlow 196
7.9 Determine All Fonts Installed on a User’s Computer 198
7.10 Display Vertical Text in a TextArea 199
7.11 Set the Selection in a TextArea 201
7.12 Control the Appearance of the Selected Text 203
7.13 Copy a Character as a Bitmap 204
7.14 Create Linked Containers in a TextFlow 207
7.15 Use a Custom Format Resolver 208
7.16 Skin the TextArea Control 212
7.17 Create Multiple Text Columns 213
7.18 Highlight the Last Character in a TextFlow 214
8. Lists and ItemRenderers . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
8.1 Create an Item Renderer for a Spark List 218
8.2 Create an Editable List 219
8.3 Scroll to an Item in a Spark List 221
8.4 Change the Layout of a Spark List 221
8.5 Create a Nested List 222
8.6 Set XML Data for a Spark List 225
8.7 Allow Only Certain Items in a Spark List to Be Selectable 228
Table of Contents | vii

www.it-ebooks.info
8.8 Format and Validate Data Added in a Spark List Item Editor 230
8.9 Create a Right-Click Menu for a Spark List 235
8.10 Enable Dragging in a Spark List 237
8.11 Customize the Drop Indicator of a Spark List 240
8.12 Display Asynchronously Loaded Data in a Spark List 244
9. DataGrid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
9.1 Create Custom Columns for a DataGrid 249
9.2 Specify Sort Functions for DataGrid Columns 253
9.3 Filter Items in a DataGrid 254
9.4 Create Custom Headers for a DataGrid 257
9.5 Handle Events from a DataGrid 260
9.6 Enable Drag and Drop in a DataGrid 264
9.7 Edit Items in a DataGrid 266
9.8 Search Within a DataGrid and Autoscroll to the Match 267
9.9 Generate a Summary for Flat Data by Using a Grouping Collection 270
9.10 Create an Async Refresh for a Grouping Collection 273
10. Video . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
10.1
Create a Basic Video Player 277
10.2 Display Video Playback Progress 278
10.3 Create a Skinned Video Player 280
10.4 Display Streaming Video 283
10.5 Display the Bytes Loaded of a Video 284
10.6 Create a Basic Video Player Using the Open Source Media
Framework 285
10.7
Access and Display Cue Points Embedded in a Video File 286
10.8 Create a Wrapper for the Open Source Media Framework 289
10.9 Display Captions with the Open Source Media Framework 290

11. Animations and Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
11.1 Dynamically Set a Filter for a Component 296
11.2 Call an Animation in MXML and in ActionScript 297
11.3 Create Show and Hide Effects for a Component 299
11.4 Define Keyframes for an Animation 301
11.5 Create Parallel Series or Sequences of Effects 302
11.6 Pause, Reverse, and Restart an Effect 304
11.7 Set Effects for Adding a Component to or Removing One from a
Parent Component 305
11.8
Create Custom Animation Effects 308
11.9 Use the DisplacementMapFilter Filter in a Flex Effect 310
11.10 Use the Convolution Filter to Create an Animation 314
11.11 Use Pixel Bender to Create a Transition 319
viii | Table of Contents
www.it-ebooks.info
12. Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
12.1 Add, Remove, or Retrieve Data from an ArrayList 323
12.2 Retrieve and Sort Data from an ArrayCollection 325
12.3 Filter an ArrayCollection 327
12.4 Determine When an Item Within an ArrayCollection Is Modified 328
12.5 Create a GroupingCollection 329
12.6 Create a Hierarchical Data Provider for a Control 332
12.7 Navigate a Collection Object and Save Your Position 336
12.8 Create a HierarchicalViewCollection Object 339
12.9 Filter and Sort an XMLListCollection 342
12.10 Sort on Multiple Fields in a Collection 344
12.11 Sort on Dates in a Collection 345
12.12 Create a Deep Copy of an ArrayCollection 347
12.13 Use Data Objects with Unique IDs 349

13. Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
13.1
Bind to a Property 353
13.2 Bind to a Function 354
13.3 Create a Bidirectional Binding 357
13.4 Bind to Properties by Using ActionScript 358
13.5 Use Bindable Property Chains 362
13.6 Bind to Properties on a XML Source by Using E4X 364
13.7 Create Customized Bindable Properties 366
13.8 Bind to a Generic Object 370
13.9 Bind to Properties on a Dynamic Class 372
14. Validation, Formatting, and Regular Expressions . .
. . . . . . . . . . . . . . . . . . . . . . . . . 379
14.1 Use Validators and Formatters with TextInput Controls 380
14.2 Create a Custom Formatter 383
14.3 Use Regular Expressions to Create an International Zip Code
Validator 384
14.4 Validate Combo Boxes and Groups of Radio Buttons 387
14.5 Show Validation Errors by Using ToolTips in a Form 389
14.6 Use Regular Expressions for Locating Email Addresses 392
14.7 Use Regular Expressions for Matching Credit Card Numbers 393
14.8 Use Regular Expressions for Validating ISBNs 393
14.9 Create Regular Expressions by Using Explicit Character Classes 394
14.10 Use Character Types in Regular Expressions 395
14.11 Match Valid IP Addresses by Using Subexpressions 397
14.12 Use Regular Expressions for Different Types of Matches 398
14.13 Match Ends or Beginnings of Lines with Regular Expressions 400
14.14 Use Back-References 400
14.15 Use a Look-Ahead or Look-Behind 402
Table of Contents | ix

www.it-ebooks.info
15. Working with Services and Server-Side Communication . . . . . . . . . . . . . . . . . . . . . 405
15.1 Configure a HTTPService 406
15.2 Use RESTful Communication Between Flex Applications 408
15.3 Communicate with a Service That Returns JSON-Formatted Data 410
15.4 Configure Services for an Application Using BlazeDS 413
15.5 Configure and Connect to a RemoteObject 417
15.6 Use Publish/Subscribe Messaging for Chat Applications 420
15.7 Use the IExternalizable Interface for Custom Serialization 426
15.8 Track Results from Multiple Simultaneous Service Calls 427
15.9 Register a Server-Side Data Type Within a Flex Application 429
15.10 Communicate with a WebService 431
15.11 Add a SOAP Header to a Request to a WebService 433
15.12 Parse a SOAP Response from a WebService 434
15.13 Communicate Securely with AMF by Using SecureAMFChannel 435
15.14 Send and Receive Binary Data via a Binary Socket 437
15.15 Communicate Using a XMLSocket 438
15.16 Navigate a XML Document in E4X 439
15.17 Use Regular Expressions in E4X Queries 441
15.18 Add a XMLList to a XML Object 442
15.19 Handle Namespaces in XML Returned by a Service 443
15.20 Encode an ActionScript Data Object as XML 444
15.21 Decode XML from a Web Service into Strongly Typed Objects 446
16. Browser Communication . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
16.1 Link to an External URL 449
16.2 Work with FlashVars 450
16.3 Invoke JavaScript Functions from Flex 452
16.4 Invoke ActionScript Functions from JavaScript 453
16.5 Change the HTML Page Title via BrowserManager 455

16.6 Parse the URL via BrowserManager 456
16.7 Deep-Link to Data via BrowserManager 458
16.8 Deep-Link Containers via BrowserManager 460
17. Modules and Runtime Shared Libraries . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
17.1 Create a Runtime Shared Library 464
17.2 Use Cross-Domain Runtime Shared Libraries 467
17.3 Optimize a Runtime Shared Library 469
17.4 Create a MXML-Based Module 470
17.5 Create an ActionScript-Based Module 472
17.6 Use ModuleLoader to Load Modules 474
17.7 Use ModuleManager to Load Modules 476
17.8 Load Modules from Different Servers 479
17.9 Communicate with a Module 481
x | Table of Contents
www.it-ebooks.info
17.10 Use Query Strings to Pass Data to Modules 486
17.11 Use Linker Reports to Optimize Modules 488
18. AIR Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
18.1 Create and Run an AIR Application with Flash Builder 4 491
18.2 Sign and Export an AIR Application 494
18.3 Sign an AIR File with a Trusted Certificate 497
18.4 Targeting a Specific Version of AIR 499
18.5 Set the Application ID 499
18.6 Set the Application Name and Filename 500
18.7 Set the Application Version 502
18.8 Edit the Application Description and Copyright Information 503
18.9 Edit the Initial Window Settings 505
18.10 Set the Installation Folder for an Application 507
18.11 Set the Default Programs Menu Folder 508

18.12 Set a Custom Application Icon 509
18.13 Allow an AIR Application to Interact with the Browser 511
18.14 Set the Application to Handle All Updates 512
18.15 Determine the Application Version at Runtime 514
18.16 Create Multilingual AIR Installations 514
18.17 Create Applications with Update Capabilities 516
18.18 Create Applications with Update Capabilities with a Custom
Interface 522
18.19
Package an Application in a Native Installer (.exe, .dmg, .rpm) 530
18.20 Include Native Code Within Your AIR Application 531
19. Working with Data in AIR . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537
19.1 Safeguard Files with the Encrypted Local Store 537
19.2 Migrate Serialization Changes 540
19.3 Create an In-Memory Database 542
19.4 Encrypt a Database with a Password 543
19.5 Use Parameters in Queries 546
19.6 Include a Database in an Application 552
19.7 Store Simple Relationships with an Object Relational Mapping 553
20. Operating System Integration with AIR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561
20.1 Close All Open Windows at Once 562
20.2 Add a Drop Shadow for a Custom Chrome Window 563
20.3 Use Deferred Rendering with Clipboard Data 569
20.4 Create Custom Clipboard Data Formats 570
20.5 Assign Keyboard Shortcuts to Menu Items 574
20.6 Notify the User Through the Dock (Mac) and the Taskbar
(Windows) 575
Table of Contents | xi
www.it-ebooks.info

20.7 Register Custom File Types 578
20.8 Open a File with Its Default Application 581
20.9 Check for Mounted and Unmounted Drives 585
20.10 Obtain a List of Available External Drives 586
20.11 Tell the Operating System That a File Has Been Downloaded from
the Web 593
20.12 Deploy an AIR Application as a Native Installer 594
20.13 Create a HTTP Proxy Using the ServerSocket Class 601
21. Charting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609
21.1 Create a Chart 609
21.2 Add Effects to Charts 612
21.3 Select Regions of a Chart 615
21.4 Format Tick Marks for a Chart 617
21.5 Create a Custom Label for a Chart 619
21.6 Create a Drill-Down Effect for a Column Chart 621
21.7 Skin Chart Items 624
21.8 Use ActionScript to Dynamically Add Columns to and Remove
Columns from a Chart 626
21.9 Overlap Multiple ChartSeries 630
21.10 Drag and Drop Items in a Chart 631
21.11 Create an Editable Line Chart 633
22. Unit Testing with FlexUnit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 637
22.1 Create an Application That Uses the FlexUnit Framework 638
22.2 Create an Application to Run FlexUnit Tests 638
22.3 Create a FlexUnit Test Case 641
22.4 Run Code Before and After Every Test 644
22.5 Share Test Data Between Test Cases 647
22.6 Handle Events in a Test Case 649
22.7 Test Visual Components with FlexUnit 651
22.8 Create Mock Objects for Testing 661

22.9 Use Complex Assertions in a Test Case 664
23. Compiling, Debugging, and Deploying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667
23.1 Use trace Statements Without Flash Builder 667
23.2 Use the Component Compiler 669
23.3 Install the Flex Ant Tasks 670
23.4 Use mxmlc and Ant to Compile Flex Applications 672
23.5 Use Ant to Compile and Deploy Flex Applications That Use RSLs 674
23.6 Use Rake to Compile Flex Applications 676
23.7 Create and Monitor Expressions in the Flash Builder Debugger 677
23.8 Install the Ant View in the Standalone Version of Flash Builder 680
xii | Table of Contents
www.it-ebooks.info
23.9 Use ASDoc and Ant to Generate Documentation 681
23.10 Use Express Install for Your Application 682
23.11 Use Memory Profiling with Flash Builder to View
Memory Snapshots 684
23.12 Check the Performance of Specific Methods 686
24. Internationalization, Accessibility, and Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . 689
24.1 Add an International Character Set to an Application 689
24.2 Use a Resource Bundle to Localize an Application 692
24.3 Use the ResourceManager for Localization 696
24.4 Use Resource Modules for Localization 697
24.5 Support Input Method Editor (IME) Devices 700
24.6 Detect a Screen Reader 702
24.7 Create a Tabbing Reading Order for Accessibility 703
24.8 Print Selected Items in an Application 704
24.9 Format Application Content for Printing 706
24.10 Control Printing of Unknown-Length Content over Multiple Pages 707
24.11 Add a Header and a Footer When Printing 709
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713

Table of Contents | xiii
www.it-ebooks.info
www.it-ebooks.info
Preface
Flex 4 is a powerful framework that provides enterprise-level components for the Flash
Player platform in a markup language format recognizable to anyone with HTML or
XML development experience. The Flex Framework provides components for visual
layout, visual effects, data grids, server communication, charts, and much more.
To put a blunt point on it, the Flex Framework is massive, and any book attempting
to cover the entire Framework in any depth will without question fail in some respect
or another. With this in mind, we’ve made an attempt to cover the topics that most
vex developers working with Flex 4. Along the way, we’ll illuminate how the Frame-
work is structured, as well as helping developers familiar with earlier versions of Flex
to start working with the new components and styling functionality in Flex 4. The
official Flex documentation is quite good at explaining in depth how particular methods
or classes behave, so our focus instead is on how to tackle common tasks within the
Flex Framework, how to get different components to work together, and how Flex can
partner with other technologies to create Rich Internet Applications (RIA) and more.
With the help of Adobe AIR, for example, you can use the tools of Flex and the Flash
Player to create deployable desktop applications. This complements the expansion of
open source and commercial tools for Java, .NET, and PHP development, among oth-
ers, making Flex a powerful solution for an ever-wider range of development needs and
challenges.
Who This Book Is For
Flex 4 Cookbook is for developers who want to understand the Flex Framework more
thoroughly, who need a reference to consult to solve particular problems, or who are
looking to understand new additions to the Flex Framework. As such, this book as-
sumes that you have some previous experience with Flex and ActionScript 3. The code
samples and explanations are geared toward intermediate developers familiar with the
relationship between MXML and ActionScript, with at least some of the components

that make up the Flex Framework, and with basic Flex development strategies.
xv
www.it-ebooks.info
We have made a very deliberate decision to ensure that all the recipes contain usable
components and functional, tested implementations of those components. This was
not done with the intention of swelling the book unreasonably, but to ensure that this
book is suitable for intermediate and advanced developers who simply need to see a
small code snippet to understand a technique, as well as readers who are still learning
how the Flex Framework can be used and the best practices for working with it.
Who This Book Is Not For
If you need to learn the Flex Framework from scratch, consult Programming Flex 3 by
Joey Lott and Chafic Kazoun (O’Reilly) or Hello! Flex by Peter Armstrong (Manning)
to gain an understanding of the core concepts of Flex development before reading any
further here. With a grounding in Flex and ActionScript basics, you’ll be better prepared
to take advantage of the techniques in this book. If you need a refresher course in
ActionScript development or are looking to learn techniques focused on core Flash
ActionScript programming, try ActionScript 3.0 Cookbook by Joey Lott, Darron Schall,
and Keith Peters (O’Reilly). Although Flex 4 Cookbook covers some areas of overlap
between the Flex Framework and core Flash ActionScript classes, this book is very
much focused on Flex development.
How This Book Is Organized
As its name implies, Flex 4 Cookbook is stuffed full with recipes intended to teach you
techniques that will help you get more from your Flex applications. To help you find
the solutions you need faster, the recipes are organized by theme. Generally, within
each chapter, the recipes progress from simpler to more complex topics.
This book was not intended to be read from cover to cover, but rather to be used as a
reference for particular problems, and to provide insight into particular aspects of the
Flex Framework. The recipes also include complete component implementations to
show you how to implement the concepts that are discussed. You should be able to
use the demonstrated code in your own applications or, at the very minimum, adapt

relevant portions of the code to your needs.
Conventions Used in This Book
The following typographical conventions are used in this book:
Italic
Indicates new terms, URLs, filenames, and file extensions. Also used for emphasis.
xvi | Preface
www.it-ebooks.info
Constant width
Used for program listings, as well as within paragraphs to refer to program elements
such as variable or function names, tags and components, data types, environment
variables, statements, and keywords.
Constant width bold
Shows commands or other text that should be typed literally by the user. Also used
for emphasis in code listings.
Constant width italic
Shows text that should be replaced with user-supplied values or by values deter-
mined by context.
This icon signifies a tip, suggestion, or general note.
This icon indicates a warning or caution.
Using Code Examples
This book is here to help you get your job done. In general, you may use the code in
this book in your programs and documentation. You do not need to contact us for
permission unless you’re reproducing a significant portion of the code. For example,
writing a program that uses several chunks of code from this book does not require
permission. Selling or distributing a CD-ROM of examples from O’Reilly books does
require permission. Answering a question by citing this book and quoting example
code does not require permission. Incorporating a significant amount of example code
from this book into your product’s documentation does require permission.
We appreciate, but do not require, attribution. An attribution usually includes the title,
author, publisher, and ISBN. For example: “Flex 4 Cookbook by Joshua Noble, Todd

Anderson, Garth Braithwaite, Marco Casario, and Rich Tretola. Copyright 2010 O’Re-
illy Media, Inc., 978-0-596-80561-6.”
If you feel your use of code examples falls outside fair use or the permission given above,
feel free to contact us at
Preface | xvii
www.it-ebooks.info
How to Use This Book
Think of this book as a friend and a counselor. Don’t put it on a shelf. Keep it on your
desk where you can consult it often. When you are uncertain as to how something
works or how to approach a specific programming issue, pick up the book and flip to
the relevant recipe(s). We have written this book in a format so that you can get answers
to specific questions quickly. And since it’s a book, you don’t ever have to worry that
it will laugh at you for asking questions. No question is too big or too small.
Although you can read the book from cover to cover, we encourage you to use this
book when you need an answer. Rather than teaching you a bunch of theory, this book
intends to help you solve problems and accomplish tasks. This book is meant for field-
work, not the research lab.
O’Reilly Cookbooks
Looking for the right ingredients to solve a programming problem? Look no further
than O’Reilly Cookbooks. Each cookbook contains hundreds of programming recipes
and includes hundreds of scripts, programs, and command sequences you can use to
solve specific problems.
The recipes you’ll find in an O’Reilly Cookbook follow a simple formula:
Problem
Each Problem addressed in an O’Reilly Cookbook is clearly stated, specific, and
practical.
Solution
The Solution is easy to understand and implement.
Discussion
The Discussion clarifies and explains the context of the Problem and the Solution.

It also contains sample code to show you how to get the job done. Best of all, all
of the sample code you see in this O’Reilly Cookbook can be downloaded from
the book’s website, at />See Also
The See Also section directs you to additional information related to the topic
covered in the recipe. You’ll find pointers to other recipes in the book, to other
books (including non-O’Reilly titles), websites, and more.
To learn more about the O’Reilly Cookbook series, or to find other Cookbooks that
are up your alley, visit the website at .
xviii | Preface
www.it-ebooks.info
Safari® Books Online
Safari Books Online is an on-demand digital library that lets you easily
search over 7,500 technology and creative reference books and videos to
find the answers you need quickly.
With a subscription, you can read any page and watch any video from our library online.
Read books on your cell phone and mobile devices. Access new titles before they are
available for print, get exclusive access to manuscripts in development, and post feed-
back for the authors. Copy and paste code samples, organize your favorites, download
chapters, bookmark key sections, create notes, print out pages, and benefit from tons
of other time-saving features.
O’Reilly Media has uploaded this book to the Safari Books Online service. To have full
digital access to this book and others on similar topics from O’Reilly and other pub-
lishers, sign up for free at .
How to Contact Us
Please address comments and questions concerning this book to the publisher:
O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the United States or Canada)
707-829-0515 (international or local)

707-829-0104 (fax)
We have a web page for this book, where we list errata, examples, and any additional
information. You can access this page at:
/>To comment or ask technical questions about this book, send email to:

For more information about our books, conferences, Resource Centers, and the
O’Reilly Network, see our website at:

Preface | xix
www.it-ebooks.info
Acknowledgments
This book truly does represent a product of the Flex community. Thanks are due to
many developers and the community relations managers at Adobe, to Matt Chotin, Ely
Greenfield, and Alex Harui in particular, as well as to the developers who work with
Adobe products, and have contributed to the Flex Cookbook site or blogged about
what they’ve discovered. Without all of them, this book would not have been conceiv-
able.
Many, many thanks are also due to the many people at O’Reilly who made this book
possible. Many special thanks go to Steve Weiss, Mary Treseler, Linda LaFlamme, and
Michele Filshie for their hard work, flexibility, and patience throughout the writing
and editing of this book.
The quality of the technical information within this book is not simply due to the
knowledge of its many authors. The technical reviewers for this book—Jodie
O’Rourke, Ed Mansouri, Kevin Suttle, Mykola Bilokonsky, Chuck Freedman, Russ
Ferguson, and Sean Moore—not only provided help debugging, correcting, and clari-
fying the code for this book, but also provided fantastic insight into ways to clarify
explanations, structure chapters, alter recipes, and help the readers’ understanding.
From Joshua
First and foremost, I need to thank Joey Lott and Steve Weiss for so graciously helping
me get the opportunity to write my first book so many years ago and for all the won-

derful opportunities that has provided me since. To my co-authors, Todd Anderson
and Garth Braithwaite, and also the writers of the Adobe AIR 1.5 Cookbook, Rich Tre-
tola and Marco Casario, this book would have been absolutely impossible without you.
The same goes for everyone who has participated in the Adobe Cookbook site and on
forums like FlexCoders, making a vibrant, helpful community that helps us all.
I’d also like to thank my friends whom I’ve known from jobs and from life for providing
me with so much help, advice, support, and humor. Finally, I’d like to thank my
brother, my father, and in particular my mother, for always providing me with encour-
agement, support, wisdom, and humor.
From Todd
I would first like to thank Josh Noble for asking me to participate in this book and for
providing knowledge, patience, and humor throughout. I’d also like to thank Joey Lott
for his huge encouragement and belief in people’s abilities. I’d like to thank my friends
and the Flash community for offering advice, a few laughs, and expertise. And finally
to my family, I cannot thank you enough for the huge love and support.
xx | Preface
www.it-ebooks.info
From Rich
I would like to thank my wife and best friend Kim, who has always been there for me
and has been supportive of my many ventures. I would also like to thank my daughters
Skye, Coral, and Trinity. I love you all very much! Also, thanks for the hard work of all
of my co-authors.
From Garth
I was only able to be part of this project because of Steve Weiss, Josh Noble, and Todd
Anderson; I thank them for the opportunity. Additionally, I thank my father for my
love of programming, and Dr. Paul Merril for teaching the courses that led to my pro-
fession. I’d like to thank my RIA Radio co-hosts for being a part of my weekly fanboy
fest: Leif Wells, Zach Stepek, and Stacey Mulcahy. I also need to thank the Adobe
community, and particularly Rachel Luxemburg, Edward Sullivan, and Greg Hamer,
for their encouragement. Finally, I am nothing without the support of my wife, daugh-

ters, mom, and family.
From Marco
Special thanks to my fantastic co-authors for the quality and the amount of work they
put into this book. I would also like to thank my colleagues at Comtaste—without their
hard work on our internal projects, I would never have achieved what I have done. My
sincere and deep thanks to the crew at O'Reilly for their patience, persistent assistance,
and professionalism throughout the entire process.
I welcome conversation and comment on this book—email me at m.casario@
comtaste.com, or leave a comment on my blogs at or http://
casario.blogs.com.
Preface | xxi
www.it-ebooks.info
www.it-ebooks.info
CHAPTER 1
Flex and ActionScript Basics
A Flex application consists primarily of code written in two different languages: Ac-
tionScript and MXML. In its 3.0 incarnation, ActionScript went from a prototype-based
scripting language to a fully object-oriented, strictly typed language. MXML is a markup
language that will feel comfortable to anyone who has spent time working with
Hypertext Markup Language (HTML), Extensible Markup Language (XML), or a host
of newer markup-based languages.
Many newcomers to Flex wonder how MXML and ActionScript relate to one another.
The MXML compiler (mxmlc), after parsing through the different idioms, translates
them into the same objects, so that this:
<s:Button id="btn" label="My Button" height="100"/>
and this:
var btn:Button = new Button();
btn.label = "My Button";
btn.height = 100;
produce the same object. The major difference is that while creating that object in

ActionScript (the second example) creates the button and nothing else, creating the
object in MXML adds the button to whatever component contains the MXML code.
The Flex Framework handles calling the constructor of the object described in MXML
and either adding it to the parent or setting it as a property of the parent.
MXML files can include ActionScript within a <fx:Script> tag, but ActionScript files
cannot include MXML. Although it’s tempting to think of MXML as describing the
appearance and components that make up your application and of ActionScript as
describing the event handlers and custom logic your application requires, this is not
always true. A far better way to think of their relationship is that both languages
ultimately describe the same objects via different syntax. Certain aspects of the Flash
platform cannot be accessed without using ActionScript loops, function declarations,
and conditional statements, among many other features. Consequently, the use of Ac-
tionScript, and the integration between MXML and ActionScript, is necessary for all
but the very simplest applications.
1
www.it-ebooks.info

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×