6 Replies Latest reply on Oct 8, 2008 9:24 AM by wvxvw

    Passing a variable inside an addEventListener

    wkolcz Level 1
      I want to pass a 'petID' variable to a function called by an addEventListener

      When I try:
      imageUploader.addEventListener("imageAdded", refreshImages(petID));

      I get:
      Implicit coercion of a value of type void to an unrelated type Function

      How can I pass the variable?
        • 1. Re: Passing a variable inside an addEventListener
          wvxvw Level 1
          You can not to pass additional arguments into handler function because it is invoked by EventDispatcher instance, and you have no control over how it is invoked.
          However, you may use event.target to get reference to the dispatcher where event originates, event.currentTarget to get the reference to the last dispatcher that dispatched the event into your handler function and this keyword to access the scope of the object which declared the handler function. It will be useful to have a look at flash.utils.Dictionary class in case you need to be able to distinguish from multiple event targets that may dispatch the same event into same function.
          Eg.:
          Say, you're using a set of buttons with the same handler, so, you will create a hash (Dictionary) and add them to the hash like so:

          var hash:Dictionary = new Dictionary();
          hash[myButton] = myKey;

          and you want to make myKey an iterator, or a value picked from array of keys - you choose.

          then, inside the handler function you'd check if the target is registered with hash and what is it's key in the hash and act accordingly (imagine that myKey could take values 0-9, and your buttons are stored inside array of that length) than inside the handler function:

          if (hash[event.target] != null)
          {
          (myButtonsArray[hash[event.target]] as MyButton).doWhatMyButtonsDo();
          }
          • 2. Re: Passing a variable inside an addEventListener
            wkolcz Level 1
            OR imageUploader.addEventListener('imageAdded', function(event) { refreshImages(petData.getItemAt(0).petID) }); isn't giving me any errors.
            • 3. Re: Passing a variable inside an addEventListener
              g-s-b Level 1

              ...OR, use a custom event maybe.

              • 4. Passing a variable inside an addEventListener
                wvxvw Level 1
                @projectproofing:
                Your suggested way is not advisable. It is because you add an anonymous function as a listener, which A. creates a new function each time you do it (so, you'll have a bunch of copies of the same function instead of having only 1 copy). B. makes it difficult to unsubscribe from that event (because anonymous function can be referenced only from within them self). C. makes the code sloppy and difficult to understand.
                • 5. Re: Passing a variable inside an addEventListener
                  Level 7

                  "wvxvw" <webforumsuser@macromedia.com> wrote in message
                  news:gcigml$6km$1@forums.macromedia.com...
                  > @projectproofing:
                  > Your suggested way is not advisable. It is because you add an anonymous
                  > function as a listener, which A. creates a new function each time you do
                  > it
                  > (so, you'll have a bunch of copies of the same function instead of having
                  > only
                  > 1 copy). B. makes it difficult to unsubscribe from that event (because
                  > anonymous function cannot be referenced). C. makes the code sloppy and
                  > difficult to understand.

                  That wasn't his suggestion. His suggestion was more along the lines of
                  http://flexdiary.blogspot.com/2007/08/dispatching-events.html


                  • 6. Re: Passing a variable inside an addEventListener
                    wvxvw Level 1
                    I din't mean the way one may name an event... forgive me if my post was unclear on that. It refers solely to the handler declaration within aaddEventListener function, which is inadvisable.
                    And that small codesnippet, even though it doesn't generate errors, must generate warning because of missing return type of the event handler function and type declaration of event handler argument..