views:

2257

answers:

3

Hi

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!

+1  A: 

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.

Richard Szalay
This was a very simplified example. My real situation is a custom event that does indeed use the bubbles argument. What I realise now is that you can only bubble events upwards and have to re-dispatch them if you want to go "sideways"
adam
Also worth noting is that bubbling only works on UI elements.
Richard Szalay
+1  A: 

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);
}
//...
}
quoo
A: 

Hi

Is it possible this way (or somehow else) to trigger event from DataGrid control to its custom item renderer? So the item renderer is a child of data grid.

What I need is to set a style of item renderer based on a value which is available in dataProvider of data grid control.

Is there an easy way how to do it?

Thx

Marek Branicky