Dispatching Events

To broadcast an event from a component, you need to use the dispatchEvent() method. This method is defined in the flash.events.EventDispatcher class, which is a superclass in the hierarchy from which UIComponent inherits.

The following is the inheritance hierarchy of the UIComponent class:

  mx.core.UIComponent extends   flash.display.Sprite extends   flash.display.DisplayObjectContainer extends   flash.display.InteractiveObject extends   flash.display.DisplayObject extends   flash.events.EventDispatcher 

The dispatchEvent() method takes a single argument, which is an event object to be dispatched. When an event is dispatched, anything listening for that event is notified, and the specified event handlers are executed. This offers a much better alternative to tightly coupled architectures.


Open CategorizedProductManager.mxml from your flexGrocer/managers directory.

If you skipped the lesson when this was created, you can open this file from the Lesson09/start/managers directory, and save it in your flexGrocer/managers directory.


At the end of the prodByCategoryHandler() method, find and delete the lines of code which explicitly call categorizedProductDataLoaded() in the parent. The lines to remove are as follows:

if(this.parentDocument.categorizedProductDataLoaded != null){    this.parentDocument.categorizedProductDataLoaded(aCats); } 

Here, you are eliminating the bad practice of tightly coupling this component to its parent.


Create a new instance of the event object, with a type catDataLoaded.

var e:Event = new Event("catDataLoaded"); 

This creates the new event object, which will be used in place of the tight coupling.


Just after creating the event object, dispatch it. Save this component.


This dispatches the event so that any listening components can hear and respond to it.


Open DataEntry.mxml from your flexGrocer directory.


Find the instantiation of the CategorizedProductManager component. Listen for the catDataLoadedEvent and call the categorizedProductDataLoaded() method to handle the event.

<m:CategorizedProductManager     catDataLoaded="categorizedProductDataLoaded()"/> 

Don't be alarmed if you see a problem listed in the Problems panel when you save the file, it will be explained in step 8 and fixed in the next exercise.


Find the categorizedProductDataLoaded() method in the <mx:Script> block. Make the function private and remove the argument from it. Change the line setting of the categories property so that it's set to an ArrayCollection based on prodMgr.getCats() instead of on the argument you removed.

private function categorizedProductDataLoaded():void{    categories=new ArrayCollection(prodMgr.getCats() );    foodColl=prodMgr.getCategorizedProducts(); } 

You are now repurposing the method you wrote in Lesson 8, "Using Controls and Repeaters with Data Sets." You are no longer explicitly calling this method from the data manager; instead, this method is being invoked as a handler for the catDataLoaded event. As such, it no longer needs to be public. The array of categories is not passed into this method, so the array collection will be created using the getCats() method of the manager.


Following best practices, if a method or property doesn't need to be public, it should not be.

As was mentioned in the previous step, if you save DataEntry.mxml now, you will see an error listed in the Problems panel. This is completely expected, and will be explained in the next step and fixed in the next exercise.


Save DataEntry.mxml. Look at the Problems panel and notice that an error exists.

The Problems panel is now showing an error: Cannot resolve attribute 'catDataLoaded' for component type managers.CategorizedProductManager. This error occurs because you are referring to an attribute of the CategorizedProductManager tag named catDataLoaded, but the compiler doesn't recognize any properties or events of that component with the name catDataLoaded.

For the compiler to know what catDataLoaded means, you need to add metadata to the component, specifically declaring any events that the component will dispatch.

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