Identifying the Need for Custom Event Classes


In the previous exercise, custom events were used to notify other parts of the application about a change in data. In addition to notifications, you sometimes need to pass data around with events. The base flash.events.Event class doesn't support this, but you can create an event subclass that does. The applications you have been building need a few different custom events to function properly. When all is said and done, several user tasks will generate events, for example:

In the DataEntry application:

  • Adding a new product

  • Updating an existing product

  • Deleting a product

In the Ecomm application:

  • Browsing for products by category

  • Adding a product to a shopping cart

  • Removing a product from a shopping cart

If you analyze these events carefully, you will see they all are events that need to pass along data, so the system can react properly (which product to add to the cart, which category to show, and so on). In fact, all these specifically have either a Category or a Product as their data. To facilitate this, you will create two event classes, ProductEvent and CategoryEvent.

So far, all the events you have used are instances of the built-in flash.events.Event class, which does not have a property to enable you to pass data along with an event; it has properties for things such as a type to uniquely identify the event and bubbles, which will be discussed later in this lesson.

As you saw earlier, you can broadcast an event from a component using the dispatchEvent() method.

Considering that dispatchEvent() accepts an event instance as an argument, any custom event classes you create should be a subclass of Event. You can add any methods or properties you need to your event, but you are required to override the clone() method. Overriding a method allows you to redefine a method from the superclass, for your new subclass. This allows you the flexibility to use the functionality you want from the superclass, as well as the ability to define custom functionality for your class. When you override a method, it needs to match the name, access modifier (public, protected, internal, etc.), return type, and argument list of the method from the superclass you are overriding. The clone() method returns a new copy of the Event object with the same values by setting the properties in the clone() method. Typically, you define the clone() method to return an instance of your newly created event class.




Adobe Flex 2.Training from the Source
Adobe Flex 2: Training from the Source
ISBN: 032142316X
EAN: 2147483647
Year: 2006
Pages: 225

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net