简体   繁体   中英

FLEX, Actionscript: how can I invoke a CustomEvent?

I've 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 the parameter tags from both Actionscript and MXML:

From actionscript I'm 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'm doing this and it doesn't work as well:

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

thanks

Try wrapping the 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 issue with the MXML code is that you are using a repeater and trying to get the currentItem after the repeating has finished. Try this instead:

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

Hope that helps.

Update

Since you are creating the newTag object in a loop, you'll get better memory usage by just using a named function as the event listener.

newTag.addEventListener(MouseEvent.MOUSE_UP, onTagClick);

...

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

That way you only create one event listener, rather than n listeners that do that exact same thing.

also, you may be getting TypeErrors for not having overridden the clone method. You should fix that now, before you run into it later.

greetz
back2dos

Have you tried changing type to something other than a type that is currently being used. Something like CustomMouseEvent.MY_CUSTOM_MOUSE and then catch that to see if this works. Not sure if using the same Type name as a standard type is a good technique.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM