FLEX, Actionscript: how can I call a CustomEvent?

I created a custom MouseEvent in Flex:

package {

    import flash.events.MouseEvent; 
    public class CustomMouseEvent extends MouseEvent {

        public var tags:Array = new Array();    
        public function CustomMouseEvent(type:String, tags:Array) {
            super(type, true);
            this.tags = tags;
        }
    }
   }

      

Now I would like to understand how to pass parameter tags from ActionScript and MXML:

From actioncript I am trying something like this, but it doesn't work:

newTag.addEventListener(MouseEvent.MOUSE_UP, dispatchEvent(new CustomMouseEvent(MouseEvent.MOUSE_UP,[newTag.name])));

      

From MXML I do this and it doesn't work:

<mx:LinkButton click="dispatchEvent(new CustomMouseEvent(MouseEvent.MOUSE_UP, bookmarksRepeater.currentItem.tags))" />

      

thanks

+2


a source to share


3 answers


Try to wrap your callback code in a function:

newTag.addEventListener(MouseEvent.MOUSE_UP, function(e:MouseEvent):void {
    dispatchEvent(new CustomMouseEvent(MouseEvent.MOUSE_UP, [e.currentTarget.name]));
});

      

I think the problem with the MXML code is that you are using a repeater and are trying to get currentItem

after the repeat is complete. Try this instead:

<mx:LinkButton click="dispatchEvent(new CustomMouseEvent(MouseEvent.MOUSE_UP, event.currentTarget.getRepeaterItem().tags))" />

      

Hope it helps.



Update

Since you create the object newTag

in a loop, you will get better memory usage simply by using a named function as an event listener.

newTag.addEventListener(MouseEvent.MOUSE_UP, onTagClick);

...

protected function onTagClick(e:MouseEvent):void {
    dispatchEvent(new CustomMouseEvent(MouseEvent.MOUSE_UP, [e.currentTarget.name]));
}

      

This way, you create only one event listener, not n

listeners that do the same.

+3


a source


also you can get TypeErrors in order not to override the clone method. You must fix this now before you come across it later.



Greetz
back2dos

+2


a source


You tried to change the type to something other than the type that is currently in use. Something like CustomMouseEvent.MY_CUSTOM_MOUSE and then catch this to see if it works. Not sure if using the same standard type type name is a good technique.

0


a source







All Articles