5 Replies Latest reply on Feb 19, 2009 5:00 PM by Gregory Lafrance

    Custom Event simple question

    cpthk Level 1
      I am reading some custom event tutorial from here: http://blog.olivermerk.ca/index.cfm/2007/5/23/Flex-Custom-Events . It basically break steps down into:

      1. metadata
      <mx:Metadata>
      [ Event( name="myEvent", type="flash.events.Event") ]
      </mx:Metadata>

      2. create action script:
      private function onButtonClick():void {

      var myEventObj:Event = new Event( "myEvent" );
      dispatchEvent( myEventObj );
      }
      ...
      <mx:Button x="10" y="10" label="Button" click="onButtonClick()"/>

      3.
      <comp:MyComponent id="myComponent" myCustomEvent="onMyCustomEvent( event );" />

      4.
      private function myCustomEvent( event:Event ):void {
      mx.controls.Alert.show('done.');
      }

      I don't understand how the code runs, can someone give a brief explanation?
        • 1. Re: Custom Event simple question
          Level 7
          It's giving an example of something that you could do to dispatch the
          event. It assumes that you've already put an event listener that calls
          onButtonClick() on a subcomponent of your component.
          • 2. Re: Custom Event simple question
            Gregory Lafrance Level 6
            1. metadata
            This specifies to Flex that your event myEvent should be treated as an event.

            2. create action script:

            This shows how the Button click results in a standard Flex event being dispatched (MouseEvent.CLICK), and that within the handler of that event you manually dispatch an instance of your custom event.

            3. This should how you register an event listener for your custom event for some component.

            4. This shows what should happen when the listener for the custom event event is called.

            So in essence, you declare that you will use a custom event, you dispatch the custom event in the listener of a Flex event, and some listener added to a component gets called.

            FB3 help sys on custom events will help further understand the flow.
            • 3. Re: Custom Event simple question
              cpthk Level 1
              My question is that, to users, they see no difference to a click event. Then what is the benefit of doing this? What is so special of doing this?
              My guessing is that main component can be triggered and call the myCustomEvent and run some actions. In the other hand, if using the built-in click event, I can only call some function inside the custom component? Is that the benefit of using custom event?
              • 4. Re: Custom Event simple question
                Level 7

                "cpthk" <webforumsuser@macromedia.com> wrote in message
                news:gnk5jg$e6i$1@forums.macromedia.com...
                > My question is that, to users, they see no difference to a click event.
                > Then
                > what is the benefit of doing this? What is so special of doing this?
                > My guessing is that main component can be triggered and call the
                > myCustomEvent
                > and run some actions. In the other hand, if using the built-in click
                > event, I
                > can only call some function inside the custom component? Is that the
                > benefit of
                > using custom event?

                The parent component can listen for a click event on a child of your custom
                component, sort of. It can listen for a click event on your custom
                component and then check if the target is of the class that it wants, and
                then if it is the correct one. But if that component has children itself,
                that can get to be a problem (such as a button...if you click on its label,
                when you test to see if the target is a button, it isn't).

                So this makes everyone's life a lot easier by giving specific information
                about what happened in your component.


                • 5. Re: Custom Event simple question
                  Gregory Lafrance Level 6
                  The point here is that the parent of the component can listen for the custom event and do something specific. If you have five buttons in the custom component, the parent of the component can do five different thiings depending on what custom event it gets.

                  If you just use the click event, the parent of the component has to figure out what the label of the event currentTarget is, and perhaps also check the parent of the Button, because the parent of the component dispatching the event may have several custom components in it.