4 Replies Latest reply on Jun 4, 2007 11:42 PM by QuickStick

    Listener calls Function plus Arguments??

      Hi guys,

      Not sure if there's an easy way to do this, I'd like a listener to call a function with arguments when triggered.

      Currently, what I have looks like this:

      object.addEventListener(errorTrigger, functionToCall);

      public function functionToCall():void {

      ...you get my idea...


      And, I'd really like to add some arguments to the function. I realize that if I use:

      object.addEventListener(errorTrigger, functionToCall(myArgument));

      then it will want functionToCall to return a function name to be used. Hopefully, you're still with me on this...

      The reason I want to add the arguments, is because I have three different listeners.

      And I'd rather not have three different functions to deal with them, I'd rather consolidate it into one function and use the argument to distinguish the difference.

      Rather than:

      object.listener(error1, function1);
      object.listener(error2, function2);
      object.listener(error3, function3);

      function1 ():void {}
      function1 ():void {}
      function1 ():void {}

      I'd prefer something like:

      object.listener(error1, function(1));
      object.listener(error2, function(2));
      object.listener(error3, function(3));

      function(num):void {}

      Hopefully, that all makes sense. Sorry, I'd post the code, but it's way to long, and I'd rather not confuse anyone with something else contained within it.

      More than happy to try and cut it down if someone needs to see the code though.

        • 1. Re: Listener calls Function plus Arguments??

          You are supose to pass all the parametes through the EVENT object, all you have to do is a custom event object that will hold whatever you want to pass to the listener.

          The right way to use this is
          obj.addEventListener(someEventName, listenerFunction);

          private function listenerFunction(evt:customEvent):void {

          To call the listner
          obj.dispatchEvent(new customEvent(someEventName, customParameter);
          • 2. Re: Listener calls Function plus Arguments??
            QuickStick Level 1
            Thanks for your efforts, I don't quite know whether it's what I'm looking for though.

            The events I am using are predefined by Flex's upload function. So, this may help:

            fileUpload.addEventListener(HTTPStatusEvent.HTTP_STATUS, uploadError);
            fileUpload.addEventListener(IOErrorEvent.IO_ERROR, uploadError);
            fileUpload.addEventListener(SecurityErrorEvent.SECURITY_ERROR, uploadError);

            And I'd really like to add the parameters to the end, like so:

            fileUpload.addEventListener(HTTPStatusEvent.HTTP_STATUS, uploadError(param));
            fileUpload.addEventListener(IOErrorEvent.IO_ERROR, uploadError(param));
            fileUpload.addEventListener(SecurityErrorEvent.SECURITY_ERROR, uploadError(param));

            Is this something that I have to cast to / extend apon the existing events using your technique above? In essence, creating a duplicate of this event?

            • 3. Re: Listener calls Function plus Arguments??
              buabco Level 1
              I'm not sure what you are trying to do, but it simply doesn't work that way. Even if you do a custom event, it wont work, since the one who dispatchs the event is the one creating the event object. In this case you will keep getting the normal event with no parameter. May be there is another way to do what you want but you'll have to explain what the main problem is.
              • 4. Re: Listener calls Function plus Arguments??
                QuickStick Level 1
                Thanks anyway, I have it all working, I was just hoping there was a tidier way.

                Help appreciated.