11 Replies Latest reply on Aug 19, 2008 10:56 AM by Newsgroup_User

    Custom event passing data

    istrasci
      I've searched a lot for help on this, so I apologize if I've overlooked something or a simple answer has eluded me... Still, your help would be much appreciated...

      I'm trying to set up a custom event that will pass along an ArrayCollection with it to anyone listening for the event... Specifically, custom components that might need this ArrayCollection... My basic structure is as follows:

      Main/parent component
      - Insert a Metadata tag declaring that I can send out this event
      - Some action (button click, etc.) triggering a handler function that instantiates the custom event, and then dispatches it (my testing and debugging shows that it's being dispatched successfully).

      Custom component
      - At "creationComplete" event, trigger a handler function that sets up an event listener, i.e., parent.addEventListener(myType, myHandlerFunction)
      - myHandlerFunction assigns the ArrayCollection contained in the event to a local ArrayCollection (I'm aware that assigning one ArrayCollection to another assigns a reference)

      Please let me know if you see any obvious errors in my code/logic... Here's the code for the custom event:
        • 1. Re: Custom event passing data
          Level 7

          "istrasci" <webforumsuser@macromedia.com> wrote in message
          news:g8d02a$8cp$1@forums.macromedia.com...
          > I've searched a lot for help on this, so I apologize if I've overlooked
          > something or a simple answer has eluded me... Still, your help would be
          > much
          > appreciated...
          >
          > I'm trying to set up a custom event that will pass along an
          > ArrayCollection
          > with it to anyone listening for the event... Specifically, custom
          > components
          > that might need this ArrayCollection... My basic structure is as follows:
          >
          > Main/parent component
          > - Insert a Metadata tag declaring that I can send out this event
          > - Some action (button click, etc.) triggering a handler function that
          > instantiates the custom event, and then dispatches it (my testing and
          > debugging
          > shows that it's being dispatched successfully).
          >
          > Custom component
          > - At "creationComplete" event, trigger a handler function that sets up an
          > event listener, i.e., parent.addEventListener(myType, myHandlerFunction)
          > - myHandlerFunction assigns the ArrayCollection contained in the event to
          > a
          > local ArrayCollection (I'm aware that assigning one ArrayCollection to
          > another
          > assigns a reference)
          >
          > Please let me know if you see any obvious errors in my code/logic...
          > Here's
          > the code for the custom event:
          >
          > package myPackage
          > {
          > import flash.events.Event;
          > import mx.collections.ArrayCollection;
          >
          > public class ArrayCollectionEvent extends Event
          > {
          > public var itemDescription:String;
          > public var ac:ArrayCollection
          > public static const ARRAYCOLLECTIONEVENT:String = "ArrayCollectionEvent";
          >
          > public function ArrayCollectionEvent(description:String,
          > ac:ArrayCollection)
          > {
          > super(ARRAYCOLLECTIONEVENT,true,true);
          > this.itemDescription = description;
          > this.ac = ac;
          > }
          >
          > override public function clone():Event
          > {
          > return new ArrayCollectionEvent(this.itemDescription, this.ac); //
          > bubbling
          > support inside
          > }
          > }
          > }

          Depending on what you're trying to do, this may not be necessary. Most
          default Flex events carry enough references where you can get at public
          properties of the target or currentTarget.

          Care to elaborate on the full game plan?


          • 2. Re: Custom event passing data
            istrasci Level 1
            quote:

            Depending on what you're trying to do, this may not be necessary. Most
            default Flex events carry enough references where you can get at public
            properties of the target or currentTarget.

            Care to elaborate on the full game plan?


            The component has a combobox in it... I wish for the combobox to change its dataProvider to a new ArrayCollection that comes in on my custom event... The problem for now is not accessing the ArrayCollection, but that the event is not being caught by the component, despite the fact that the component's addEventListener and the parent's dispatching the event both appear successful...

            • 3. Re: Custom event passing data
              Level 7

              "istrasci" <webforumsuser@macromedia.com> wrote in message
              news:g8elcn$e7i$1@forums.macromedia.com...
              >
              quote:

              Depending on what you're trying to do, this may not be necessary. Most
              > default Flex events carry enough references where you can get at public
              > properties of the target or currentTarget.
              >
              > Care to elaborate on the full game plan?

              >
              > The component has a combobox in it... I wish for the combobox to change
              > its
              > dataProvider to a new ArrayCollection that comes in on my custom event...
              > The
              > problem for now is not accessing the ArrayCollection, but that the event
              > is not
              > being caught by the component, despite the fact that the component's
              > addEventListener and the parent's dispatching the event both appear
              > successful...

              Have the parent set a property on the child. On the child side, instead of
              using a public variable, use a getter/setter pair of functions. In the
              setter function, change the dataprovider on the combobox.

              HTH;

              Amy


              • 4. Re: Custom event passing data
                istrasci Level 1
                quote:

                Have the parent set a property on the child. On the child side, instead of
                using a public variable, use a getter/setter pair of functions. In the
                setter function, change the dataprovider on the combobox.

                HTH;

                Amy



                Yeah, I guess I can use that to do what I need, but my focus is trying to learn about custom events and being able to pass data... I'm still confused as to why the component isn't "catching" the dispatched event... New focus... Someone help me answer that question... Forget about the array and data...
                • 5. Re: Custom event passing data
                  Level 7

                  "istrasci" <webforumsuser@macromedia.com> wrote in message
                  news:g8eo6g$ho3$1@forums.macromedia.com...
                  >
                  quote:

                  Have the parent set a property on the child. On the child side,
                  > instead of
                  > using a public variable, use a getter/setter pair of functions. In the
                  > setter function, change the dataprovider on the combobox.
                  >
                  > HTH;
                  >
                  > Amy

                  >
                  >
                  > Yeah, I guess I can use that to do what I need, but my focus is trying to
                  > learn about custom events and being able to pass data... I'm still
                  > confused as
                  > to why the component isn't "catching" the dispatched event... New
                  > focus...
                  > Someone help me answer that question... Forget about the array and
                  > data...

                  Events don't bubble downward. Have you added the event listener directly to
                  the parent component? If so, this is a bad practice ;-)


                  • 6. Re: Custom event passing data
                    istrasci Level 1
                    quote:

                    Events don't bubble downward. Have you added the event listener directly to
                    the parent component? If so, this is a bad practice ;-)


                    Well, the component calls: parent.addEventListener(myType, myHandlerFunction)

                    I just did it this way because I was following an example (and the example worked, no less)...

                    I guess I don't really understand where events live when they are dispatched then... I thought they were just thrown out there, and then (like radio) anyone listening to the same "frequency" could hear the event and do whatever they need to do... Is that not correct...???

                    If I shouldn't add the listener directly to the parent component, where is a better place...? this.addEventListener ???

                    • 7. Re: Custom event passing data
                      Level 7

                      "istrasci" <webforumsuser@macromedia.com> wrote in message
                      news:g8epc9$j74$1@forums.macromedia.com...
                      >
                      quote:

                      Events don't bubble downward. Have you added the event listener
                      > directly to
                      > the parent component? If so, this is a bad practice ;-)

                      >
                      > Well, the component calls: parent.addEventListener(myType,
                      > myHandlerFunction)
                      >
                      > I just did it this way because I was following an example (and the example
                      > worked, no less)...
                      >
                      > I guess I don't really understand where events live when they are
                      > dispatched
                      > then... I thought they were just thrown out there, and then (like radio)
                      > anyone listening to the same "frequency" could hear the event and do
                      > whatever
                      > they need to do... Is that not correct...???
                      >
                      > If I shouldn't add the listener directly to the parent component, where is
                      > a
                      > better place...? this.addEventListener ???

                      Child components should not know or need to know anything about their
                      parents. Instead, either the parents should give them information on their
                      properties or they should be bound to a common model that everyone is using.
                      http://www.adobe.com/devnet/flex/articles/graduating_pt1.html


                      • 8. Re: Custom event passing data
                        istrasci Level 1
                        quote:

                        Child components should not know or need to know anything about their
                        parents. Instead, either the parents should give them information on their
                        properties or they should be bound to a common model that everyone is using.
                        http://www.adobe.com/devnet/flex/articles/graduating_pt1.html


                        Yeah, I'm aware of that... That's why I'm trying to learn to use events... My component doesn't know about anybody else... So he needs to listen on the off-chance that someone may be talking to him... And if he hears someone, then he'll act accordingly...

                        I added parent.addEventListener because I was just following another example (like I said)... I think that it's fair for a component to assume it'll have a parent; if not, the component can never exist because no one will instantiate it... Anyway, even if I do this.addEventListener in the component, it still doesn't catch the event...

                        Anyway, you're not answering my question about the logic... Component registers listener, parent (or other) dispatches the event... Am I missing something??? Why isn't it being caught be the component...

                        • 9. Re: Custom event passing data
                          Level 7

                          "istrasci" <webforumsuser@macromedia.com> wrote in message
                          news:g8erpj$map$1@forums.macromedia.com...
                          >
                          quote:

                          Child components should not know or need to know anything about their
                          > parents. Instead, either the parents should give them information on
                          > their
                          > properties or they should be bound to a common model that everyone is
                          > using.
                          > http://www.adobe.com/devnet/flex/articles/graduating_pt1.html

                          >
                          > Yeah, I'm aware of that... That's why I'm trying to learn to use
                          > events...
                          > My component doesn't know about anybody else... So he needs to listen on
                          > the
                          > off-chance that someone may be talking to him... And if he hears someone,
                          > then
                          > he'll act accordingly...
                          >
                          > I added parent.addEventListener because I was just following another
                          > example
                          > (like I said)... I think that it's fair for a component to assume it'll
                          > have a
                          > parent; if not, the component can never exist because no one will
                          > instantiate
                          > it... Anyway, even if I do this.addEventListener in the component, it
                          > still
                          > doesn't catch the event...
                          >
                          > Anyway, you're not answering my question about the logic... Component
                          > registers listener, parent (or other) dispatches the event... Am I
                          > missing
                          > something??? Why isn't it being caught be the component...

                          What kind of event is it? It may be that a child component can't get that
                          kind of event.


                          • 10. Re: Custom event passing data
                            istrasci Level 1
                            quote:

                            What kind of event is it? It may be that a child component can't get that
                            kind of event.


                            It's a custom event... What do you mean by "can't get that kind of event"...??? Can't any type of component listen for any type of event...?
                            • 11. Re: Custom event passing data
                              Level 7

                              "istrasci" <webforumsuser@macromedia.com> wrote in message
                              news:g8eub8$pk0$1@forums.macromedia.com...
                              >
                              quote:

                              What kind of event is it? It may be that a child component can't get
                              > that
                              > kind of event.

                              >
                              > It's a custom event... What do you mean by "can't get that kind of
                              > event"...??? Can't any type of component listen for any type of event...?

                              If it is a custom event, then the child component will need to know more
                              about the parent than just that it has a parent. Your child component
                              should not be enforcing on any potential parent that it has to have this
                              type of event.

                              But to answer your question, no, a child component cannot register for, for
                              instance, click events on the parent. As I said, events do not bubble down.