3 Replies Latest reply on Apr 6, 2007 7:05 AM by peterent

    Proper event bubbling with custom components

    romanczar
      Hi!
      I am using custom components to have something like

      Application
      -- CustomPanel
      ---- CustomComboBox

      My CustomComboBox dispatches custom events., e.g.

      <mx:Metadata>
      [Event(name="dateChanged", type="com.dashboard.events.ChangeMonthEvent")]
      </mx:Metadata>

      This works fine, I can catch the event in my CustomPanel component. But I need to catch it in Application, so I added

      <mx:Metadata>
      [Event(name="dateChanged", type="com.dashboard.events.ChangeMonthEvent")]
      </mx:Metadata>

      to CustomPanel as well. Now I can catch the event in Application

      Is this the only way to do it? When I leave out the metadata in CustomEvent I receive a compiler error.....

      Isn't there any other way? I thought bubbling was the solution, but it is not...?
        • 1. Re: Proper event bubbling with custom components
          peterent Level 2
          Event bubbling doesn't require you to listen for an event at each level, but it does require that the component be able to receive the event.

          For example, you can do: <mx:Application click="handleClick" ... /> and every click event from every component can be handled by the handleClick function. That's because Application has the click event metadata associated with it. If your CustomPanel does not have your custom event as its meta data, then you will get a compilation error because the compiler doesn't know that dateChanged is an event that CustomPanel can dispatch (even, if it if only passes through the component).

          So yes, you need the [Event("dateChanged")] metadata on the component where you will add the event listener. In other words, if you have this:

          Application
          -- CustomPanel
          ------ CustomChild1
          ----------- CustomChild2
          ------------------ CustomComboBox

          You only need [Event("dateChanged")] on CustomPanel if you only have <CustomPanel dateChanged="...." /> because the event will bubble up through the other components as long as you dispatch the event with bubbles=true. CustomChild1 and CustomChild2 do not have to have the metadata.
          • 2. Re: Proper event bubbling with custom components
            romanczar Level 1
            But CustomComboBox has to have the metadata?
            • 3. Re: Proper event bubbling with custom components
              peterent Level 2
              Not necessarily. Only if you want it to be referenced at compilation time. Something like:

              <CustomComboBox dateChanged="handleEvent()" ... />

              You can dispatch events which are not named by metadata.