Flex AS3 – Dispatching Event between classes

actionscript-3apache-flex

I thought I had custom events nailed in Flex, but seemingly not. I am trying to loosely couple my application by dispatching events between classes instead of using horrid parent.parent.parent statements.

I have a ComboBox inside a custom HBox class… for simplicity I am doing the following

public function onComboBoxChange(event:MouseEvent):void {
    trace("dispatching event");
    dispatchEvent(new Event("hello"));
}

I have a custom List class that I would like to respond to the event…

public function CustomList() {
    //...
    this.addEventListener(FlexEvent.INITIALIZE, onInit);
}

private function onInit(event:FlexEvent):void {
    this.addEventListener("hello", onHello);
}

private function onHello(event:Event):void {
    trace("oh hello");
}

However the event listener is never called.

Both CustomList and CustomHBox have the same parent.

I was under the impression you could dispatchEvent from any object and all other active objects would be able to listen for it. Not that simple?

Thanks!

Best Answer

Your list either needs to call addEventListener("hello") on the combobox directly, or the combobox needs to dispatch the event with a bubbles argument of true.

Your concept of events is missing 'bubbling' you can read more about events in flash on the Adobe site.

Related Topic