« March 2006 | Main | December 2006 »

July 17, 2006

The Incomplete Guide to Flex Events (Part 1 of 4)

When building Flex2 applications, developers have four (4) categories of events that may need to be considered in their custom applications: (1) system events, (2) user events, (3) business events, and (4) data service events.

I personally think the term system event is ambiguous. For example does " system" refer to the OS system or the FlashPlayer system or the MX Framework system... such ambiguity forces me remind the reader that these events are the Flex 2 Framework system events. In the sebsequent paragraphs, we will learn about these four event categories and how they impact our thoughts regarding Flex development.

The Flex2 framework provides superb native support for system, user, and data service events. Developers must create their own business events and must take special care to use the data service events appropriately. This 4-part guide really focuses on the concepts and uses of business and data service event categories. Before we can properly discuss such topics, however, let's first review the concepts and issues with system and user events.

SECTION 1: System Events

We assume the Flex developer has a base familiarity with system and user events, but may not be aware of the distinctions between the system and user event categories. System events are events that are never generated directly by the user. Instead these events are generated by code activity within the mx framework; events that usually occur during application startup, component creation, component layout/measuring, and more. Such events include creationComplete, initialize, show, hide, etc.

Developers may "hook" or capture these events using built-in event stubs and custom event handlers. The event stub is the location where you specify your event handler and usually has the same name as the event itself. The Flex2 framework provides a multitude of built-in event stubs for both system and user events.


In examples 1 and 2 below the event "stub" [or hook location] is the creationComplete="". The event "handler" is the code inside the "" on the right side of the creationComplete stub. In our examples, we want to insert code that will be executed when the areaAlert component announces that its creation process is done or completed. Example 1 uses inline actionscript to specify our code. Example 2 uses specifies a custom function or method that will be executed when the event handler is invoked. If the event stub is empty [ "" ] - in other words, does not have an event handler - then the creationComplete event is still fired by the component but no event handler is executed.

Below are two equivalent examples of system event hooks:


Example 1


Example 2

Please note: as a course of "best practices", inline actionscript is fine for short 1 code segments [like above]. Explicit method calls as event handlers - e.g. announceReady() - are better used for multiple code segments. This keeps your <mx .../> tags clean and easily managed. Example 3 below shows an multi-line event handler.


Example 3

In various portions of my article I will identify features of the Flex2 platform where compiler MAGIC occurs. By this, I mean activity such as custom tags that are transparently converted to actionscript code or inline actionscript code that is converted during compiler processes... all these can be viewed as "MAGIC" because we never see it happening and it saves us - the developer - an amazing amount of manual work; thus allowing us to keep our code uncluttered with tedious details.

Earlier we stated that examples 1 and 2 are equivalent. When the Flex code in Example 1 is compiled, the inline actionscript is first converted to a distinct method [analogous to the announceReady() method shown in the script section of Example 2]. But you never see this hidden conversion process.... MAGIC!

My next posting will continue with Part 2 of 4: User Events... so stay tuned!

July 16, 2006

Flex Data Services - FlexStore and Inventory Demo

I recently presented at the CFUnited 2006 Conference. My 1-hour presentation was titled "Flex 2.0 Data Services (FDS) and ColdFusion – 203". During that presentation I outlined the major features of FDS and how they integrate with Coldfusion web applications. I discussed which features FDS features were automatically available in ColdFusion 7.02 Enterprise: specifically HTTPService and Remoting to CFCs. I also tried to highlight when a development project will need to obtain a separate licensed copy of the full FDS installation.

A copy of that presentation is available for download here: Download PowerPoint Presentation.

At the end of my slide presentation, I then showed the audience a demonstration of Flex Real-time Messaging using two Flex applications: the FlexStore and a sample Inventory Dashboard applications. This demo was originally created by Robert Tiernay @ Abobe. With his permission I presented a modified version. Here is the scenario presented:

    • The user opens a web browser and uses the FlexStore application to purchase Nokia phones. After the user places 1 or more phones items into his shopping cart, he can then select the 'Submit' order button.

    FlexStore Image

    • Meanwhile, the store manager is using the Inventory Dashboard application to get real-time updates regarding the current inventory status. The Dashboard app also shows him when the inventory quantity is below a "threshold" level and more Nokia phones should be ordered.

    Inventory Image

The interesting features of this demo are (1) the dashboard never needs to refresh to get updated data, and the dashboard is NOT continually polling the server for updates... instead (2) the server "pushes" inventory changes whenever an order is submitted. In fact the demo FlexStore has a batch mode that randomly changes the quantities of each item in the shopping cart and then incrementally submits the order for each item separately. This allows users to see the Inventory Dashboard change as multiple orders/inventory changes are submitted over a period of time.

At least 20 attendees asked for source code to that demo. That source and the presentation slides - along with a video of the presentation - will be available online at the CFUnited site soon. In the interim, here is the source code for the FlexStore Ordering - Live Inventory Dashboard demo(s).  I have included in the .zip archive some configuration instructions with the source. I hope you enjoy the demo(s) and the thought-provoking ideas that it generates. 

Included with the source code are instructions for the installation steps required to properly install and run the attached FDS demo(s): those instructions include details for both client and server configurations. Click here to download the FlexStore/Inventory Dashboard source code.