Reputation: 22597
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!
Upvotes: 1
Views: 6585
Reputation: 6307
You should still be fine if your event bubbles. The parent of CustomList and CustomHBox will add an event listener to CustomHBox for the event you're dispatching from the onComboBoxChange. The event handler should be in this parent class, and it'll pass the event / execute whatever code needs to be executed in CustomList ie:
public class Main {
public var customList:CustomList;
public var customHBox:CustomHBox;
//...
public function init():void {
customHBox.addEventListener(MyCustomEvent.EVENT_NAME, myCustomEventHandler, false, 0, true);
}
//...
public function myCustomEventHandler(event:MyCustomEvent):void {
customList.update(event.data);
}
//...
}
Upvotes: 1
Reputation: 84824
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.
Upvotes: 1