1

Is it possible to pass a var at the end of an addEventListener?

/// clickType declared elsewhere in code.
checkBoxFast.addEventListener(clickType, goFast("yes"));

function goFast(evt:Event=null,myVar:String)
{
trace(myVar);
}
Papa De Beau
  • 3,744
  • 18
  • 79
  • 137
  • Look at this question: http://stackoverflow.com/questions/12590082/how-to-dispatch-an-event-with-added-data-as3/12590659#comment16968983_12590659 – BadFeelingAboutThis Sep 28 '12 at 23:41

4 Answers4

2

I guess if you want to parametrize your event handing I would suggest passing variables to the Event.

-Create a custom event:

public class MyEvent extends Event {
    public var myVar:String;

    public function MyEventHistoryEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false) {
        super(type, bubbles, cancelable);
    }
}

-Dispatch this event from the event dispatcher with the required variable:

var event:MyEvent = new MyEvent("eventType");
event.myVar = "yes";

dispatchEvent(event);

-Add an event handler:

checkBoxFast.addEventListener("eventType", eventHandler);

protected function eventHandler(event:MyEvent):void {
trace(event.myVar);
}
Max Golovanchuk
  • 757
  • 6
  • 14
1

Another solution would be to use an anonymous function like so:

checkBoxFast.addEventListener(clickType, function(e:Event):void{goFast("yes")});

function goFast(myVar:String)
{
    trace(myVar);
}
Amit Dvir
  • 337
  • 1
  • 3
  • 9
  • Anonymous functions as a event listener - is a huge source of memory leaks. It is impossible to removeEventListener with anonymous function. – Max Golovanchuk Nov 22 '12 at 07:06
  • @MaxGolovanchuk Not true. If the event handler is only meant to fire once, you can add `IEventDispatcher(e.currentTarget).removeEventListener(e.type, arguments.callee);` inside your anonymous function. If it is meant to be used multiple times, add a boolean in there to decide whether to add it back again. Even better, there are plenty of Event Managers out there to help manage event listeners. Correct use can give you a much better handle on your anonymous functions as event listeners, and memory leaks in general. I wrote one myself which I intend to publish. Will share a link here when I do. – Amit Dvir Nov 22 '12 at 12:43
  • @Amit Dvir. Anonymous functions are evil, as they can be used a closures and contain the reference to the variables inside the object is it enclosing and thus preventing GC to clear the things. :) And adding a removing functionality inside an anonymous function is not a solution - what if you want to remove the function somewhere else not inside the anonymous function? About Event Managers - don't you thing it is a bit and overkill to use a custom manager for a simple event handling. – Max Golovanchuk Nov 22 '12 at 16:16
  • @MaxGolovanchuk Yes, but if your app is big enough to be worried about memory leaks, it would be a good idea to implement some sort of Event Manager, regardless of whether you use anonymous functions or not. I have found anonymous event handlers (and event managing) to be absolute lifesavers, when used correctly. – Amit Dvir Nov 26 '12 at 08:25
0

Creating custom event is best way I guess. But I was using sometimes different aproach. I dont know if it is good practice but it works in some cases.

public function test() {

        var myVar : String = "some value";
        addEventListener(MouseEvent.CLICK, onClick);


        function(e:Event){
            trace(myVar);
        }

    }
Riddlah
  • 302
  • 3
  • 15
0

Here's a pretty clean way:

checkBoxFast.addEventListener(clickType, goFast("yes"));

function goFast(myVar:String) {return function(e:Event) {
    trace(myVar);
}}

BUT beware anonymous functions, they won't let you end the listener in the same place it was made! If you keep repeating it like that many times in your application, it may get slow and freeze.

Actually, I really recommend you to do it like this:

var functionGoFast:Function = goFast("yes");
checkBoxFast.addEventListener(clickType, functionGoFast);

function goFast(myVar:String):Function {
  return function(evt:Event = null):void {
    trace(myVar);
  }
}

//checkBoxFast.removeEventListener(clickType, functionGoFast);

See this answer for more examples and explanations on your case.

Community
  • 1
  • 1