代码之家  ›  专栏  ›  技术社区  ›  adam

flex as3-在类之间调度事件

  •  1
  • adam  · 技术社区  · 16 年前

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

    我在一个自定义的HBox类中有一个组合框…为了简单起见,我正在执行以下操作

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

    我有一个自定义列表类,我想对事件作出响应…

    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");
    }
    

    但是,从不调用事件侦听器。

    CustomList和CustomHBox都有相同的父级。

    我觉得你可以从任何对象发送信息,其他所有活动对象都能听到。不是那么简单吗?

    谢谢!

    2 回复  |  直到 15 年前
        1
  •  1
  •   Richard Szalay    16 年前

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

    您的事件概念缺少“冒泡”,您可以在 Adobe site .

        2
  •  1
  •   quoo    16 年前

    如果你的活动泡汤了,你还是可以的。CustomList和CustomHBox的父级将为从OnComboxChange发送的事件向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);
    }
    //...
    }
    
    推荐文章