ptg ptg Adobe ® Flex ® 4.5 Fundamentals Training from the Source Michael Labriola Jeff Tapper Matthew Boles Foreword by Adam Lehman, Adobe Flash Builder Product Manager ptg Adobe® Flex® 4.5 Fundamentals: Training from the Source Michael Labriola/Jeff Tapper/Matthew Boles is Adobe Press book is published by Peachpit. For information on Adobe Press books, contact: Peachpit 1249 Eighth Street Berkeley, CA 94710 510/524-2178 510/524-2221 (fax) For the latest on Adobe Press books, go to www.adobepress.com To report errors, please send a note to errata@peachpit.com Copyright © 2012 by Michael Labriola and Jerey Tapper Adobe Press Editor: Victor Gavenda Project Editor: Nancy Peterson Development Editor: Robyn G. omas Technical Editor: Steve Lund Production Coordinator: Becky Winter Copy Editor: Jessica Grogan Compositor: Danielle Foster Indexer: Emily Glossbrenner Cover Design: Peachpit Press Notice of Rights All rights reserved. No part of this book may be reproduced or transmitted in any form by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. For infor- mation on getting permission for reprints and excerpts, contact permissions@peachpit.com. Notice of Liability e information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of the book, neither the authors, Adobe Systems, Inc., nor the publisher shall have any lia- bility to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the instructions contained in this book or by the computer soware and hardware products described in it. Trademarks Flash, ColdFusion, and Adobe are registered trademarks of Adobe Systems, Inc. Flex is a trademark of Adobe Systems, 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 Peachpit was aware of a trademark claim, the designations appear as requested by the owner of the trademark. All other product names and services identied throughout this book are used in editorial fashion only and for the benet of such companies with no intention of infringement of the trademark. No such use, or the use of any trade name, is intended to convey endorsement or other aliation with this book. Printed and bound in the United States of America ISBN 13: 978-0-321-77712-6 ISBN 10: 0-321-77712-3 9 8 7 6 5 4 3 2 1 ptg To my wife, Laura, and my daughter, Lilia; you make life much less quiet, but so much more worthwhile. —Michael Labriola My eorts on this book are dedicated to my wife, Lisa, and children, Kaliope and Kagan. Without you to inspire me, this just wouldn’t be possible. —Je Tapper To Sandra, my wife, who has made the last 25 years together a joy. And to Scrappy, my furry shing buddy. —Matthew Boles ptg Bios Michael Labriola is a Founding Partner and Senior Consultant at Digital Primates. He has been developing Internet applications since 1995 and has been working with Flex since its 1.0 beta program. Michael is a Flex SDK contributor, architect of both the open source FlexUnit and Spoon Framework projects, and international speaker on Flex and AIR topics who has consulted for many of the world’s most recognized brands. Je Tapper is a Founding Partner and Senior Consultant at Digital Primates, a company that provides expert guidance on rich Internet application development and empowers clients through mentoring. He has been developing Internet-based applications since 1995 for a myriad of clients, including Major League Baseball, ESPN, Morgan Stanley, Conde Nast, IBM, Dow Jones, American Express, Verizon, and many others. He has been developing Flex appli- cations since the earliest days of Flex 1. As an instructor, Je is certied to teach all of Adobe’s courses on Flex, AIR, Flash, and ColdFusion development. He is also a frequent speaker at Adobe Development Conferences and user groups. Matthew Boles is a Technical Training Specialist for the Adobe Enterprise Training group, and has been developing and teaching courses on Flex since the 1.0 release. Matthew has a diverse background in web development, computer networking, and teaching. He is coauthor of previous versions of this book, as well as a contributing author of the Adobe authorized Flex courseware. ptg Acknowledgments anks to Robyn, Steve, Je, and Matt for their work and dedication to this book. anks to my clients and colleagues for the motivation to keep learning new technologies. anks to my family for the unwavering support and love. Most importantly, thanks to those who inspire me every day with their words, caring, and wisdom; I promise to always keep trying. —Michael Labriola I would like to thank Mike, Matt, Steve, and Robyn for all their hard work, which has helped shape this book. anks to Chris Gieger for providing some design love for our application— Chris, sorry we couldn’t fully implement your excellent design. Special thanks go to the team at Adobe who has made this all possible. anks to the editorial sta at Adobe Press, who was faced with the Herculean task of making our writing intelligible. —Je Tapper anks to Je, Mike, and Robyn for making this the easiest book revision I’ve ever worked on! —Matthew Boles ptg Contents Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .x Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xii LESSON 1 Understanding Rich Internet Applications. . . . . . . . . . . . . . . . . . . 3 The Evolution of Computer Applications. . . . . . . . . . . . . . . . . . . . . . 4 The Break from Page-Based Architecture. . . . . . . . . . . . . . . . . . . . . . 6 The Advantages of Rich Internet Applications . . . . . . . . . . . . . . . . . . 7 RIA Technologies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 What You Have Learned. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15 LESSON 2 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17 Getting Started with Flex Application Development . . . . . . . . . . . . . .18 Creating a Project and an MXML Application . . . . . . . . . . . . . . . . . . .18 Understanding the Flash Builder Workbench . . . . . . . . . . . . . . . . . . .24 Running Your Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .28 Exploring the Flash Builder Debugger . . . . . . . . . . . . . . . . . . . . . . .34 Getting Ready for the Next Lessons . . . . . . . . . . . . . . . . . . . . . . . . .41 What You Have Learned. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43 LESSON 3 Laying Out the Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45 Learning About Layouts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .46 Laying Out the E-Commerce Application. . . . . . . . . . . . . . . . . . . . . .50 Working with Constraint-Based Layouts . . . . . . . . . . . . . . . . . . . . . .58 Working with View States . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .63 Refactoring. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .71 What You Have Learned. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .76 LESSON 4 Using Simple Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .79 Introducing Simple Controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .80 Displaying Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .81 Building a Detail View. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .85 ptg vii Training from the Source Using Data Binding to Link a Data Structure to a Simple Control . . . . . .88 Using a Form Layout Container to Lay Out Simple Controls. . . . . . . . . .89 What You Have Learned. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .92 LESSON 5 Handling Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .95 Understanding Event Handling. . . . . . . . . . . . . . . . . . . . . . . . . . . .96 Handling System Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .107 What You Have Learned. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .111 LESSON 6 Using Remote XML Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Using Embedded XML. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .114 Using XML Loaded at Runtime . . . . . . . . . . . . . . . . . . . . . . . . . . .119 Retrieving XML Data via HTTPService. . . . . . . . . . . . . . . . . . . . . . .124 Searching XML with E4X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .127 Using Dynamic XML Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .133 Using the XMLListCollection in a Flex Control. . . . . . . . . . . . . . . . . .137 What You Have Learned. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .139 LESSON 7 Creating Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 Building a Custom ActionScript Class . . . . . . . . . . . . . . . . . . . . . . .142 Building a Value Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .143 Building a Method to Create an Object. . . . . . . . . . . . . . . . . . . . . .150 Building Shopping Cart Classes. . . . . . . . . . . . . . . . . . . . . . . . . . .154 Manipulating Shopping Cart Data . . . . . . . . . . . . . . . . . . . . . . . . .159 What You Have Learned. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .167 LESSON 8 Using Data Binding and Collections . . . . . . . . . . . . . . . . . . . . . . 169 Examining Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .170 Being the Compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .176 Understanding Bindable Implications. . . . . . . . . . . . . . . . . . . . . . .183 Using ArrayCollections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .184 Refactoring ShoppingCartItem . . . . . . . . . . . . . . . . . . . . . . . . . . .204 What You Have Learned. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .205 LESSON 9 Breaking the Application into Components . . . . . . . . . . . . . . . . . 207 Introducing MXML Components . . . . . . . . . . . . . . . . . . . . . . . . . .208 Splitting Off the ShoppingView Component . . . . . . . . . . . . . . . . . .213 Breaking Out a ProductItem Component . . . . . . . . . . . . . . . . . . . .221 Creating Components to Manage Loading the Data . . . . . . . . . . . . .230 What You Have Learned. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .238 ptg viii Contents LESSON 10 Using DataGroups and Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 Using Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .242 Using DataGroups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .245 Virtualization with Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .255 Displaying Grocery Products Based on Category Selection . . . . . . . . .257 What You Have Learned. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .259 LESSON 11 Creating and Dispatching Events. . . . . . . . . . . . . . . . . . . . . . . .261 Understanding the Benefits of Loose Coupling. . . . . . . . . . . . . . . . .262 Dispatching Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .263 Declaring Events for a Component. . . . . . . . . . . . . . . . . . . . . . . . .267 Identifying the Need for Custom Event Classes. . . . . . . . . . . . . . . . .269 Building and Using the UserAcknowledgeEvent . . . . . . . . . . . . . . . .270 Understanding Event Flow and Event Bubbling . . . . . . . . . . . . . . . .274 Creating and Using the ProductEvent Class . . . . . . . . . . . . . . . . . . .280 What You Have Learned. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .289 LESSON 12 Using the Flex DataGrid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 Introducing DataGrids and Item Renderers . . . . . . . . . . . . . . . . . . .292 Displaying the ShoppingCart with a DataGrid . . . . . . . . . . . . . . . . .292 What You Have Learned. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .309 LESSON 13 Using Drag and Drop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311 Introducing the Drag and Drop Manager . . . . . . . . . . . . . . . . . . . .312 Enhanced Dragging and Dropping Between Two Lists . . . . . . . . . . . .313 Standard Dragging and Dropping Between a DataGrid and a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .315 Using a Non-Drag-Enabled Component in a Drag-and-Drop Operation . . . . . . . . . . . . . . . . . . . . . . . . . . .321 Dragging a Grocery Item to the Shopping Cart. . . . . . . . . . . . . . . . .326 What You Have Learned. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .331 LESSON 14 Implementing the Checkout Process . . . . . . . . . . . . . . . . . . . . . 333 Introducing Navigation with States . . . . . . . . . . . . . . . . . . . . . . . .334 Introducing Two-Way Bindings . . . . . . . . . . . . . . . . . . . . . . . . . . .334 Creating the OrderInfo valueObject . . . . . . . . . . . . . . . . . . . . . . . .335 Creating CheckoutView. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .337 Creating CreditCardInfo. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .345 Creating Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .350 Completing the Order. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .355 What You Have Learned. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .358 ptg ix Training from the Source LESSON 15 Using Formatters and Validators. . . . . . . . . . . . . . . . . . . . . . . .361 Introducing Formatters and Validators . . . . . . . . . . . . . . . . . . . . . .362 Using Formatter Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .364 Examining a Second Locale . . . . . . . . . . . . . . . . . . . . . . . . . . . . .368 Using Validator Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .369 What You Have Learned. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .372 LESSON 16 Customizing a Flex Application with Styles . . . . . . . . . . . . . . . . . 375 Applying a Design with Styles and Skins . . . . . . . . . . . . . . . . . . . . .376 Cleaning Up the Appearance . . . . . . . . . . . . . . . . . . . . . . . . . . . .376 Applying Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .377 Changing CSS at Runtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .395 What You Have Learned. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .397 LESSON 17 Customizing a Flex Application with Skins. . . . . . . . . . . . . . . . . .399 Understanding the Role of Skins in a Spark Component. . . . . . . . . . .400 Understanding the Relationship between Skins and States . . . . . . . . .404 Creating a Skin for the Application. . . . . . . . . . . . . . . . . . . . . . . . .413 What You Have Learned. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .417 LESSON 18 Creating Custom ActionScript Components . . . . . . . . . . . . . . . . . 419 Introducing Components with ActionScript 3.0 . . . . . . . . . . . . . . . .420 Building Components Can Be Complex. . . . . . . . . . . . . . . . . . . . . .420 Understanding Flex Components . . . . . . . . . . . . . . . . . . . . . . . . .421 Why Make Components?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .422 Defining a Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .424 Creating the Visuals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .432 Adding Functionality to the Component. . . . . . . . . . . . . . . . . . . . .439 Creating a Renderer for the Skin . . . . . . . . . . . . . . . . . . . . . . . . . .450 What You Have Learned. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .452 APPENDIX Setup Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455 Software Installation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .455 Importing Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .458 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462 [...]... Directory structure Adobe Training from the Source The Adobe Training from the Source and Adobe Advanced Training from the Source series are developed in association with Adobe and reviewed by the product support teams Ideal for active learners, the books in the Training from the Source series offer hands-on instruction designed to provide you with a solid grounding in the program’s fundamentals If you... know-how, the author team guides your journey into the exciting world of RIAs, ensuring success every step of the way Flex is powerful, highly capable, fun, and incredibly addictive And Adobe Flex 4.5: Training from the Source is the ideal tour guide on your journey to the next generation of application development Adobe Flex 4.5: Training from the Source is an update to the popular Adobe Flex 4: Training from. .. all the pieces together Adobe Flex 4.5: Training from the Source draws from the expertise of its authors to present lessons that not only introduce you to the Flex framework but also teach you the best practices you need to be successful Times are changing Whether its browser, desktop, or mobile devices, the Flex SDK and Adobe Flash Builder provides the tools you need to build a better Internet The. .. DataServices Training from the Source Most of the improvements in Flex 4.5 are focused around mobile and device development Rather than introducing a separate mobile version of Flex, we upgraded the existing framework for mobile development You can now use the same tools and languages to build a Flex mobile application that you do to build a Flex application for the browser of the desktop Built on the foundation... questions, and answers on Adobe products Adobe Marketplace & Exchange www .adobe. com/cfusion/exchange is a central resource for finding tools, services, extensions, code samples, and more to supplement and extend your Adobe products Adobe Flex product home page www .adobe. com/products /flex is the official home page from Adobe for Flex related products Adobe Labs http://labs .adobe. com gives you access... Regardless of which server-side technologies (if any) are used, the flow goes something like this: 1 The user opens a browser and requests a page from a web server 2 The web server receives the request 3 The web server hands the request to an application server to dynamically assemble the web page, or it retrieves a static page from the file system 4 The web server sends the page (dynamic or static) back to the browser 5 The browser draws the page in place of whatever was previously displayed... both the Adobe development teams building that technology and other like-minded members of the community Adobe Certification The Adobe Certified program is designed to help Adobe customers and trainers improve and promote their product-proficiency skills There are four levels of certification: • Adobe Certified Associate (ACA) • Adobe Certified Expert (ACE) • Adobe Certified Instructor (ACI) • Adobe. .. instructor, the Adobe Certified Instructor (ACI) program takes your skills to the next level and gives you access to a wide range of Adobe resources Adobe Authorized Training Centers offer instructor-led courses and training on Adobe products, employing only Adobe Certified Instructors A directory of AATCs is available at http://partners .adobe. com For information on the Adobe Certified program, visit www .adobe. com/support/certification/main.html... identify them easily Training from the Source Code block: To help you easily identify ActionScript, XML, and HTML code within the book, the code has been styled in a special font that’s different from the rest of the text Single lines of ActionScript code that are longer than the margins of the page are wrapped to the next line They are designated by an arrow at the beginning of the continuation of a broken... ActionScript Training from the Source To be a successful Flex developer, you’ll need to understand a number of concepts, including the following: • How Flex applications should be built (and how they should not) • What the relationships between MXML and ActionScript are, and when to use each • How to load data into a Flex application • How to use the Flex components, and how to write your own • What the performance . guide on your journey to the next generation ofapplication development. Adobe Flex 4. 5: Training from the Source is an update to the popular Adobe Flex 4: Training from the Source. It is our sincere. structure Adobe Training from the Source e Adobe Training from the Source and Adobe Advanced Training from the Source series are developed in association with Adobe and reviewed by the product. ptg ptg Adobe ® Flex ® 4. 5 Fundamentals Training from the Source Michael Labriola Jeff Tapper Matthew Boles Foreword by Adam Lehman, Adobe Flash Builder Product Manager ptg Adobe Flex 4. 5 Fundamentals: