views:

66

answers:

3

hi,

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

+3  A: 

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.

RJ Regenold
yeah.. I have the same problem with newTag. newTag is created in a for loop, so I newTag.name is just the name of the last tag I've added (when I trigger the event, run-time). So what's the solution ?
Patrick
Just updated my answer to show using the `currentTarget` of the event to get the correct `newTag` object. Does that work?
RJ Regenold
perfect. All my issues are solved
Patrick
+2  A: 

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

back2dos
cool thanks for the tip. +1
Patrick
A: 

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.

WeeJavaDude