Minimizing the Number of Custom Events

Until now, you’ve concentrated on building rich components for a business framework. We Flex architects also recommend some coding techniques that serve the same goal as these components: enabling application developers to write less code. In this section, you’ll see how to minimize the number of custom event classes in any application.

Flex is all about event-driven development. Create loosely coupled custom components and let them send events to each other, as in the mediator pattern example from Chapter 2. You can create new events for every occasion. If an event does not need to carry any additional data, just give it a name, specify its type as flash.events.Event, and define the meta tag to help Flash Builder list this event in its type-ahead prompts and dispatch it when appropriate. If your new event needs to carry some data, create an ActionScript class extending flash.events.Event, define a variable in this subclass to store application data, and override the method clone().

Currently, for a midsize Flex application that includes about 30 views, where each view has two components that can send/receive just one custom event, for example, you face the need to write 60 custom event classes that look pretty much the same. We’ll show you how to get away with just one custom event class for the entire application.

To illustrate the concept, we’ve created a simple application that defines one event class that can serve multiple purposes. This ...

Get Agile Enterprise Application Development with Flex now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.