views:

16

answers:

1

I have 2 files, 1 to dispatch the event and 1 to receive it (of course in addition to the event class itself).

I've done this before with both dispatching and receiving files being mxml, but this time the dispatching file is an actionscript class, and it's not working this time. Is it different for actionscript classes?

Here's a dumbed down version

The dispatching class dispatches the event as soon as it's created.

public class ASClass extends UIComponent{

     public function ASClass(){
        dispatchEvent(new MyEvents(MyEvents.FIRE_EVENT));
     }
}

in my main mxml app, I instantiate the ASClass which automatically dispatch the event as soon as it's created and the main mxml app should receive back. But something's not working.

protected function appCComplete(event:FlexEvent):void{
    addEventListener(MyEvents.FIRE_EVENT, gotEvent);
    var asClass:ASClass = new ASClass();

}

protected function gotEvent(event:MyEvents):void{
       Alert.show("got event");         
}
+2  A: 
addEventListener(MyEvents.FIRE_EVENT, gotEvent);

is same as

this.addEventListener(MyEvents.FIRE_EVENT, gotEvent);

which listens for FIRE_EVENT dispatched by this object - your main mxml app. To listen for events fired from asClass object, you have to call addEventListener on that object.

asClass.addEventListener(MyEvents.FIRE_EVENT, gotEvent);

Thus, you cannot listen for events fired from the constructor. The following code shows the correct way to listen to the events fired by the asClass object (from anywhere but the constructor).

protected function appCComplete(event:FlexEvent):void{
    var asClass:ASClass = new ASClass();
    asClass.addEventListener(MyEvents.FIRE_EVENT, gotEvent);
}

protected function gotEvent(event:MyEvents):void{
       Alert.show("got event");         
}

If you think about it, you can see that it makes sense. The constructor is used to create an object - event subscribers are normally interested in some actions/changes occurring to the object once the object is fully built and functional.

That said, you can pass a reference to this or the function to the constructor of ASClass and then assign that function as the event listener from within the constructor if you want.

    public class ASClass extends UIComponent{
        public function ASClass(listener:Function){
            this.addEventListener(MyEvents.FIRE_EVENT, listener);
            dispatchEvent(new MyEvents(MyEvents.FIRE_EVENT));
        }
    }

    var asClass:ASClass = new ASClass(this.gotEvent);//pass the function.

//or

    public class ASClass extends UIComponent{
        public function ASClass(listenerObj:Object){
            this.addEventListener(MyEvents.FIRE_EVENT, listenerObj.gotEvent);
            dispatchEvent(new MyEvents(MyEvents.FIRE_EVENT));
        }
    }

    var asClass:ASClass = new ASClass(this);//pass the object.

Personally, I can't think of many scenarios where I'd want to go this way: consider some redesigning so that you listen to the events after the constructor returns.

Amarghosh
@cooper There won't be any race conditions. The main point is that you should call event listener with the object that dispatches the event. Thus you should be calling `asClass.addEventListener` - you cannot do this before the call to the constructor as `asClass` is null at that point. Again, event listener should be added before the event is dispatched - but you can do this only after the dispatching object is created.
Amarghosh
_it won't be slower than the ASClass dispatching the event._ I'm not sure I understand you here..
Amarghosh