0
votes

I created a custom event with a custom property in actionscript. I can dispatch a custom event (in Main.mxml) with a custom property but I'm not able to listen for this event anywhere else in my application. I'm trying to listen to this event in a custom component.

When I debug I can confirm that the event gets dispatched correctly with the right value.

Snippet of Main.mxml:

var userid:String = result.charAt(4);
//dispatch a custom event in which we store the userid
var event_userid:MyEvent = new MyEvent(MyEvent.COMPLETE,userid);
dispatchEvent(event_userid);

CustomEvent class:

package ownASClass
{
    {
//http://stackoverflow.com/questions/1729470/attaching-a-property-to-an-event-in-flex-as3
    import flash.events.Event;

    public class MyEvent extends Event
    {

        public static const COMPLETE:String = "MyEventComplete";

        public var myCustomProperty:*;

        public function MyEvent(type:String, prop:*) :void
        {
            super(type,true);
            myCustomProperty = prop;

        }

        //override clone() so your event bubbles correctly
        override public function clone() :Event {

            return new MyEvent(this.type, this.myCustomProperty)

        }
    }
}       

}

I listen to this event in my custom component:

//init gets called as: initialize="init();
protected function init():void
        {
            //add evt listener for custom event to get userid
            this.parentApplication.addEventListener(MyEvent.COMPLETE,getUserid);
            //my custom component is part of a viewstack defined in main.mxml
        }

protected function getUserid(event:Event):void
        {
            //do something (but this never gets called)

        }
3
have you tried using creationComplete="init()" instead? I think that this.parentApplication may not exist when initialize event is fired (so listener never added to anything)user1901867

3 Answers

1
votes
protected function init():void
    {
        //add evt listener for custom event to get userid
        this.parentApplication.addEventListener(MyEvent.COMPLETE,getUserid);
        //my custom component is part of a viewstack defined in main.mxml
    }

Please try to change above line as follows -

this.addEventListener(MyEvent.COMPLETE,getUserid);
0
votes

Events typically 'bubble up' the chain from child windows to parent to application(main.mxml).

http://livedocs.adobe.com/flex/3/html/help.html?content=events_08.html

If you are trying to use an event to signal some sub-object you'll need to target it there and have a registered event listener.

0
votes

Used a workaround for this, realized I didn't really need to dispatch a custom event. Declared my userid in main application and called it in my component with:

public var userid:String = FlexGlobals.topLevelApplication.userid;