一个事件能走多远,我该如何倾听?

时间:2011-03-29 22:48:00

标签: actionscript-3 event-handling addeventlistener dispatchevent

我在flex组件中有代码,我想要监听一个事件,事件的来源是一个自定义类,由另一个类运行,由另一个类等运行。我的印象是事件将在整个应用程序中传递,所以我希望我在类中调度自定义事件是这样的。

    private function finishEvent():void {
        var evt:EventDispatcher = new EventDispatcher;
        var finished:Event = new Event("finishedInterpret");
        evt.dispatchEvent(finished);
    }

然后我可以在我的组件中抓住它:

public function interpret(data:Array):void {
    addEventListener("finishedInterpret", applyInferences);
    db.executeBatch();
}

事件在executeBatch完成时基本上被触发,并且正在调用finishEvent,但我是监听器没有得到任何东西。我尝试将其设置为db.addEventListener,但现在已经生效了。

1 个答案:

答案 0 :(得分:1)

事件应该发生的方式是对象调度事件,这些事件的使用者侦听来自该对象的事件 。除非您有全局事件调度程序(非典型),否则没有应用程序范围的事件调度。

我发现这是最好的模式:子组件调度事件,这些孩子的所有者会监听他们的事件。例如:

child.addEventListener("finishedInterpret", applyInferences);

按原样,您的代码正在侦听来自的事件