O'Reilly logo

Programming Flex 2 by Joey Lott, Chafic Kazoun

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Flex, Take 2

So, the biggest problem that I saw with Flex 1.0 was not with the technology per se, but with the packaging and positioning. Selling Flex as an expensive enterprise server made it irrelevant to developers who just wanted to build cool stuff in Flash. I just could not imagine anyone who went through what I did with Flash saying, "Hmmm, this isn't really for me, maybe I'll check out that multithousand-dollar enterprise presentation server." As a result, an opportunity was missed, because I had become convinced that if developers tried Flex, they would love it.

After I finished looking around, I made some recommendations as to what I thought should be done. The first was that the part of Flex used to build Flash applications (i.e., the Flex framework and compiler) should be offered separate from the server. I had no problem with the server, as it had a lot of value, but it should not be required.

I also recommended that we build a real developer-style tool for Flex that enabled a more traditional client development model. Flex 1.0 did have a development tool, called Flex Builder, but it was built as an extension to Dreamweaver and it lacked many features one expected in a real developer IDE. What I wanted was something that felt more like a tool such as Visual Studio or Eclipse.

The Flex 2 Framework

Fortunately, there was broad agreement, and my recommendations were reflected in what we actually did to create Flex 2. So, what is it?

The core of Flex 2 is the Flex framework, a library of ActionScript objects that provide a great foundation for building rich Internet applications that run on Flash. It is a developer-centric framework that provides a strong architecture and uses design patterns that will be familiar to developers coming from a .NET, Java, or web development background.

Flex 2 has a rich component model, similar to the ones found in Visual Basic, .NET, and Java. Components expose properties to enable configuration, provide methods to enable invoking their functionality, and fire events when their state changes. Flex 2 provides standard mechanisms for providing data to components, for customizing their look and feel, and for managing their layout.

But Flex doesn't just provide architecture. It also provides a wealth of useful components so that developers don't have to build everything from scratch. These include buttons, lists, menus, sliders, tabs, accordions, data grids, and more. Of course, it is easy to build your own components from scratch or customize the ones provided.

The primary way one programs with Flex is via a mix of ActionScript and an XML-based language called MXML. Each tag in MXML maps to a component, so unlike HTML, you don't have a fixed set of tags. If you write new components, you have new tags to use. Properties on a component become the tag's attributes. MXML also supports script blocks where you can put ActionScript event-handling code and utility functions.

One exciting decision we made was to provide the Flex Framework SDK, which includes the Flex framework with complete source, compilers, and other utilities, for free. We did this to encourage adoption and enable it to be freely used with non-Adobe tools. You can download it by going to the official Flex web site, http://www.flex.org.

Flex Builder 2

Flex Builder 2 is an IDE that makes using the Flex framework more productive. It provides a great code-editing environment for both ActionScript and MXML, a WYSIWYG design view to allow you to build your UI visually, a powerful debugger, and a project system that automates compiling your application.

The source editors are especially valuable because they help you to write correct code more easily and they streamline learning the framework object model. We put a lot of work into code completion to make it always up-to-date, whether it's providing suggestions for built-in classes or for ones that you create.

One of the challenges in doing this was that because MXML and ActionScript are essentially two languages defining and using the same objects, what you do in one affects the other. For example, you can define a class in ActionScript and use it from MXML, and as you make changes to the class definition, they will be reflected in the hints you are offered when editing MXML code.

Because we wanted to make Flex Builder a tool that developers would really like, we built it on the Eclipse framework as a set of plug-ins. Eclipse is a widely adopted, open source tools framework originally developed by IBM. It has a huge extension-building community, and many of its extensions are free and open source and can easily be integrated into Flex Builder 2. You can install Flex Builder as a standalone tool, or as a set of plug-ins to an existing installation of Eclipse.

ActionScript 3

One of the most important aspects of Flex 2 is that it is written entirely in ActionScript 3, which was introduced as part of Flash Player 9. Both products shipped simultaneously. ActionScript 3 is an incredibly important new language for a number of reasons.

First, ActionScript has always been based on EcmaScript, which is the standard that JavaScript is based on, but in the past was not implemented 100% to specification. To better support the standard and help it move forward, Macromedia played an active role on the EcmaScript planning committee and made ActionScript 100% compatible with the next major revision of the standard.

One thing you'll find is that this is not the JavaScript you have in today's browsers, but rather is a much more modern and robust language. In fact, I find it to be much more like C# or Java and think it will really appeal to developers coming from either of those languages. A key feature that I really like is the option of strong typing. This results in much more useful error messages and enables you to produce much more correct and reliable code.

To provide a more robust execution environment for ActionScript 3, the Flash Player team developed a new virtual machine (VM), called ActionScript Virtual Machine 2, or AVM2 for short. It was created from the ground up to be fast and scalable, and it features a just-in-time (JIT) compiler that turns the ActionScript 3 bytecode into native code. In that respect, it is much more like a Java VM or the .NET CLR than the script engines in today's browsers. The result is that it is 10 times faster than the previous VM and it uses much less memory. Note that the previous version of the VM, now called AVM1, continues to be included within Flash Player to ensure backward compatibility.

We recently made AVM2 open source by donating it to the Mozilla Foundation for incorporation into Firefox. We believe this will speed adoption of the new standard, and help ensure compatibility with future implementations of JavaScript.

Flex Data Services

The final component of Flex 2 is Flex Data Services (FDS), which represents the evolution of the original Flex server. FDS has added an incredible array of features to enable richer, more responsive applications, including client server messaging, JMS integration, a rich data model and data synchronization framework, data paging, and proxy services.

One of the most intriguing features is that FDS supports bidirectional messaging between the client and the server. This allows the server to actually push data to the client without the client having to poll for updates. This solves one of the key problems in building rich web applications for real-time data display, such as for financial services.

Although FDS is not always required when building a Flex application, it is extremely valuable when it is required. To encourage easy adoption of FDS, we created a free Express edition that allows free, nonexpiring commercial use. The only limitation is that the applications can't be clustered or run across multiple CPUs.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required