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
a source to share
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.
a source to share