Flex AS3 - 在类之间调度事件

时间:2022-08-30 11:50:03

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.

我以为我在Flex中固定了自定义事件,但似乎没有。我试图通过在类之间调度事件而不是使用可怕的parent.parent.parent语句来松散地耦合我的应用程序。

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

我在自定义HBox类中有一个ComboBox ...为简单起见,我正在做以下事情

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...

我有一个自定义List类,我想响应该事件...

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.

CustomList和CustomHBox都具有相同的父级。

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!

2 个解决方案

#1


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.

您的列表需要直接在组合框上调用addEventListener(“hello”),或者组合框需要使用bubbles参数为true来调度事件。

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

您的事件概念缺少“冒泡”,您可以在Adobe网站上阅读有关Flash中事件的更多信息。

#2


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:

如果您的活动出现泡沫,您仍应该没事。 CustomList和CustomHBox的父级将为您从onComboBoxChange调度的事件向CustomHBox添加一个事件侦听器。事件处理程序应该在这个父类中,并且它将传递事件/执行需要在CustomList中执行的任何代码,即:

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

#1


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.

您的列表需要直接在组合框上调用addEventListener(“hello”),或者组合框需要使用bubbles参数为true来调度事件。

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

您的事件概念缺少“冒泡”,您可以在Adobe网站上阅读有关Flash中事件的更多信息。

#2


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:

如果您的活动出现泡沫,您仍应该没事。 CustomList和CustomHBox的父级将为您从onComboBoxChange调度的事件向CustomHBox添加一个事件侦听器。事件处理程序应该在这个父类中,并且它将传递事件/执行需要在CustomList中执行的任何代码,即:

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