2009-07-22

Passing Additional Parameters Or Arguments Using addEventListener In Flex

With Adobe Flex one can make a lot of things. In the heart of its event-driven-programming, there is a famous function called addEventListener(). From Adobe LiveDoc we can see the syntax is like:

addEventListener() method
public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void

Parameters


type:String — The type of event.


listener:Function — The listener function that processes the event. This function must accept an Event object as its only parameter and must return nothing, as this example shows:
function(evt:Event):void

The function can have any name.



useCapture:Boolean (default = false)Determines whether the listener works in the capture phase or the target and bubbling phases. If useCapture is set to true, the listener processes the event only during the capture phase and not in the target or bubbling phase. If useCapture is false, the listener processes the event only during the target or bubbling phase. To listen for the event in all three phases, call addEventListener twice, once with useCapture set to true, then again with useCapture set to false.


priority:int (default = 0) — The priority level of the event listener. The priority is designated by a signed 32-bit integer. The higher the number, the higher the priority. All listeners with priority n are processed before listeners of priority n-1. If two or more listeners share the same priority, they are processed in the order in which they were added. The default priority is 0.


useWeakReference:Boolean (default = false) — Determines whether the reference to the listener is strong or weak. A strong reference (the default) prevents your listener from being garbage-collected. A weak reference does not.

Class-level member functions are not subject to garbage collection, so you can set useWeakReference to true for class-level member functions without subjecting them to garbage collection. If you set useWeakReference to true for a listener that is a nested inner function, the function will be garbage-collected and no longer persistent. If you create references to the inner function (save it in another variable) then it is not garbage-collected and stays persistent.


Throws

ArgumentError — The listener specified is not a function.

See also


========================================================
There is no way to pass some addtional parameters to an event handling function!

Deva Raj's solution is fine, but without some explaination it is hard to understand.

So the quick and dirty way is to use an inline function:

btn1.addEventListener(MouseEvent.CLICK, function(e:MouseEvent){myEventHandler(e, btn1.id)});

public function myEventHandler(e:MouseEvent, btnId:String) {
Alert.show("Clicked button"+btnId, "Great Title",Alert.OK);
}

Here btn1.id is just an example parameter, you can replace it with what you want.

Another better(!) solution is to create custom Event, which you can find it here:

ActionScript Code:
package com
{
import flash.events.Event;

public class EventWithData extends Event
{
public var data:*;

public function EventWithData(type:String, data:*,
bubbles:Boolean = false, cancelable:Boolean = false)
{
this.data=data;
super(type, bubbles, cancelable);
}

}
}
The mechanism details can be found also in Adobe LiveDocs: Registering event handlers

3 comments:

Anonymous said...

thanks..
nice explanation..

Sri said...

Awesome!

Thanks!

Swarna Bheemireddy said...

Nice post