7 Replies Latest reply on Aug 22, 2008 3:01 PM by VarioPegged

    Event names/types and listener types

    istrasci
      OK, so I'm really confused here... I have a component who registers an event listener to listen for double-clicks like so:

      parent.addEventListener(MouseEvent.DOUBLE_CLICK, someHandler);


      Then in my main app, I create a Metadata tag and dispatch an event like this...

      <mx:Metadata>
      [Event(name="yourMama", type="flash.events.MouseEvent")]
      </mx:Metadata>

      public function someWork():void
      {
      dispatchEvent(new MouseEvent("yourMama"));
      }


      My confusion is as follows: what do the "name" string in the Metadata tag and the event constructor have
      ANYTHING to do with the first argument to the addEventListener function (this first argument is called "type" in the Flex documentation)??? It seems like my component is listening for a "doubleClick" (value of MouseEvent.DOUBLE_CLICK) event, and the main is only throwing out "yourMama" events... Yet somehow it's working...

      The only thing I can think of is that the first argument of addEventListener (the 'type') is related to the "type" string in the Metadata tag... But if that's the case, how does the dispatcher know that it's dispatching a "doubleClick" event (which the listener is listening for)??? Nowhere in the Metadata tag or the event creation does it specify that... The most specific thing we know from either of those two things is that the event is a MouseEvent, but not what type of MouseEvent...

      If someone can clear up this confusion, it would be greatly appreciated... TIA...

      - istrasci
        • 1. Re: Event names/types and listener types
          yigitt
          The first parameter is actually a string to identify the event.

          So if you register an A objects B event, then, whenever A object throws an event B, your listener will be triggered.

          For your example, be sure that you call someWork function without any doubleClick event by user. If you call the funtion, e.g. on onCreationComplete and your listener for "doubleClick" runs, there should be a bug bcuz it is impossible.

          So to clarify, name field with the object itself identifies an event. Type is important on how you cast it on listener. Metadata is not a mandatory. Any mxml component in flex is eventDispatcher, so you can dispatch any event and name them as you like (for example you can name them dynamically and listen dynamically)

          an example:

          <mx:Text onClick="{A.dispatchEvent('clickedOnMe')}" id='A'>
          </mx:Text>

          the, you can do this:
          A.addEventListener("clickedOnMe",someListener);
          if you listen onClick event, it will also be fired when clickOnMe is fired.
          I think your confusion is that you think onClick will be stopped when another event is dispatced in it's listener.

          • 2. Re: Event names/types and listener types
            istrasci Level 1
            quote:

            Originally posted by: yigitt
            The first parameter is actually a string to identify the event.

            So if you register an A objects B event, then, whenever A object throws an event B, your listener will be triggered.

            For your example, be sure that you call someWork function without any doubleClick event by user. If you call the funtion, e.g. on onCreationComplete and your listener for "doubleClick" runs, there should be a bug bcuz it is impossible.

            So to clarify, name field with the object itself identifies an event. Type is important on how you cast it on listener. Metadata is not a mandatory. Any mxml component in flex is eventDispatcher, so you can dispatch any event and name them as you like (for example you can name them dynamically and listen dynamically)

            an example:

            <mx:Text onClick="{A.dispatchEvent('clickedOnMe')}" id='A'>
            </mx:Text>

            the, you can do this:
            A.addEventListener("clickedOnMe",someListener);
            if you listen onClick event, it will also be fired when clickOnMe is fired.
            I think your confusion is that you think onClick will be stopped when another event is dispatced in it's listener.


            I read your reply several times, but I'm still confused... Seems like it didn't really answer my question... I suppose my main question is, how does it work when the event name string and the addEventListener string are different...??? In your example, you use the same string, "clickedOnMe":

            <mx:Text onClick="{A.dispatchEvent('clickedOnMe')}" id='A'>
            </mx:Text>
            ...
            A.addEventListener("clickedOnMe",someListener);


            I don't understand how they can "connect" when the strings are different like my example:

            dispatchEvent(new MouseEvent("yourMama"));
            addEventListener(MouseEvent.DOUBLE_CLICK,..) [which is really the string "doubleClick"]
            • 3. Re: Event names/types and listener types
              yigitt Level 1
              actually what I think is that you have some bug in your test code.
              If the name of the events that is dispatched and name of the event that u listen are not the same, your listener will not be called.
              Be sure that doubleClick event is not dispatched.

              Or better, send your example working code here, then we can look in detail.

              • 4. Re: Event names/types and listener types
                istrasci Level 1
                Works exactly the way I want it to, but this is just a toy example... Run it and try it out if you want... Component receives double-click events just like I want it to, but the addEventListener string and the MouseEvent name string are clearly different...
                • 5. Re: Event names/types and listener types
                  VarioPegged Level 2
                  As far as I can see, everything works like it is supposed to.

                  You do realize that a double-click event bubbles, right? Because it bubbles, your WindowedApplication receives that event and because you're adding an event listener for the parent (WindowedApplication) within the component (SuperButton), the double-click will trigger "whatImGonnaDo" in the component.

                  Dispatching the "yourMama" event in dcHandler() has nothing to do with the component responding to the dc event. It responds because it's listening for the dc that the parent receives when the Regular Button is double-clicked.

                  BTW, I'd suggest the following minor changes to stay within the best practices guidelines for member names:
                  Use "yoMama" instead of "yourMama" ... and it's best to use a descriptive action (verb) for method and function names that perform actions, like doDatNow(), instead of a question, like whatImGonnaDo()... :)

                  TS
                  • 6. Re: Event names/types and listener types
                    istrasci Level 1
                    quote:

                    Originally posted by: VarioPegged
                    As far as I can see, everything works like it is supposed to.

                    You do realize that a double-click event bubbles, right? Because it bubbles, your WindowedApplication receives that event and because you're adding an event listener for the parent (WindowedApplication) within the component (SuperButton), the double-click will trigger "whatImGonnaDo" in the component.

                    Dispatching the "yourMama" event in dcHandler() has nothing to do with the component responding to the dc event. It responds because it's listening for the dc that the parent receives when the Regular Button is double-clicked.


                    Yes and no... I (subconsciously) knew that the double-click bubbles, but I guess I wasn't thinking about it so I didn't know that's what was triggering "whatImGonnaDo" in the component... Can I then add another listener in the component to react to "yourMama" events...? This might be obvious, but I'm new to Flex/events and this bubbling stuff confuses me sometimes...


                    quote:

                    BTW, I'd suggest the following minor changes to stay within the best practices guidelines for member names:
                    Use "yoMama" instead of "yourMama" ... and it's best to use a descriptive action (verb) for method and function names that perform actions, like doDatNow(), instead of a question, like whatImGonnaDo()... :)

                    TS


                    Haha, thanks... I'll keep those naming conventions in mind...
                    • 7. Re: Event names/types and listener types
                      VarioPegged Level 2
                      quote:

                      Can I then add another listener in the component to react to "yourMama" events...?

                      Yes you can, although I should add that the way you set the event listener for the parent within the component isn't ideal. The component will now always rely on a parent receiving a double-click event and dispatching a "yourMama" event (if you're adding the latter event listener too) to trigger the functionality you want. Maybe this is fine for your situation though.

                      Also, it's not necessary to have the Event Metadata tag in the main app. You typically use that tag or the AS version within a component so that you can refer to the event in MXML. This is why you can refer to the "click" event in something like the following:

                      <mx:Button .... click="onClick()" .... />

                      TS