9 Replies Latest reply on Feb 17, 2009 5:56 AM by Newsgroup_User

    Question on event object

    cpthk
      In the official flex tutorial. They called a function and passed in the event object, but they never used that object in that function. Why is that? I have realized several times in Day1 and Day2 tutorials. Here is the tutorial video: Link
      Here is partial of the code:
      <mx:Script>
      <![CDATA[
      import mx.collections.ArrayCollection;
      import mx.rpc.events.ResultEvent;

      [Bindable]
      private var account:ArrayCollection;

      private function resultHandler(event:ResultEvent):void {
      employeeData = event.result.employees.employee;
      }
      ]]>
      </mx:Script>

      <mx:HTTPService id="employeeService" url="data/employees.xml result="resultHandler(event)" />
        • 1. Re: Question on event object
          Level 7

          "cpthk" <webforumsuser@macromedia.com> wrote in message
          news:gnble3$lq0$1@forums.macromedia.com...
          > In the official flex tutorial. They called a function and passed in the
          > event
          > object, but they never used that object in that function. Why is that? I
          > have
          > realized several times in Day1 and Day2 tutorials. Here is the tutorial
          > video:
          > http://link.brightcove.com/services/player/bcpid1681730258?bctid=1681669348
          > Here is partial of the code:
          > <mx:Script>
          > <![CDATA[
          > import mx.collections.ArrayCollection;
          > import mx.rpc.events.ResultEvent;
          >
          > [Bindable]
          > private var account:ArrayCollection;
          >
          > private function resultHandler(event:ResultEvent):void {
          > employeeData = event.result.employees.employee;
          > }
          > ]]>
          > </mx:Script>
          >
          > <mx:HTTPService id="employeeService" url="data/employees.xml
          > result="resultHandler(event)" />

          What do you think is going on in event.result.employees.employee? Looks
          like using the event to me...


          • 2. Re: Question on event object
            cpthk Level 1
            sorry, I posted a bad example.
            I should said this video: http://link.brightcove.com/services/player/bcpid1681730258?bctid=1606789346
            • 3. Re: Question on event object
              injpix Level 3
              I see. In that video, they have a method called, “populateMailAddress”. It has a parameter called, “event” of MouseEvent type. Since this method does not assign a default value for its parameter (event), it is then required (or it will throw an error) to have a value in the call expression's argument:

              populateMailAddress(event);//this will work
              populateMailAddress();//this will NOT work

              //this parameter is known as a, “required method parameter”
              public function populateMailAddress(event:MouseEvent):void
              {

              }

              Technically, you can for the method's parameter assign it a default value, such as:

              populateMailAddress(event);//this will work
              populateMailAddress();//this will work

              public function populateMailAddress(event:MouseEvent = null):void
              {

              }

              But for event handlers like these I believe, it is not a standard.
              • 4. Re: Question on event object
                cpthk Level 1
                Thanks for your help myIP. But I still don't understand why without passing the event will not work. Because in the even previous video, here: http://link.brightcove.com/services/player/bcpid1681730258?bctid=1606789337

                In this video, she has:
                <mx:CheckBox id="samAsHome" ... click="populateMailAddress()">

                private function populateMailAddress():void
                {
                }

                In this code, the copying event still work.My question is why the next video: http://link.brightcove.com/services/player/bcpid1681730258?bctid=1606789346 , She needs to change the code by passing event into populateMailAddress, and then she never used the variable event.

                I have c++, java programming experience. From my understanding, if the function does not need the variable, then we don't need to pass that variable into the function. We only pass the variables that the function NEEDS them.
                • 5. Re: Question on event object
                  Level 7
                  In this video, they're adding an event listener using Actionscript. When
                  you add an event listener in AS, you have to pass it (as the minimal
                  arguments) an event type and a handler function. This handler function
                  is a callback: when the event is dispatched, the callback will be
                  called. When it is, it will be called with a single argument: the event
                  that triggered the callback. It will not use any other arguments, and it
                  will fail if the function you gave it as the callback cannot take that
                  argument.

                  It is possible to create an anonymous function that will call something
                  else, and use that as your callback, but this is considered bad form, as
                  you cannot remove the event listener later.
                  • 6. Re: Question on event object
                    Level 7

                    "cpthk" <webforumsuser@macromedia.com> wrote in message
                    news:gnd09l$dfe$1@forums.macromedia.com...
                    > Thanks for your help myIP. But I still don't understand why without
                    > passing the
                    > event will not work. Because in the even previous video, here:
                    > http://link.brightcove.com/services/player/bcpid1681730258?bctid=1606789337
                    >
                    > In this video, she has:
                    > <mx:CheckBox id="samAsHome" ... click="populateMailAddress()">
                    >
                    > private function populateMailAddress():void
                    > {
                    > }
                    >
                    > In this code, the copying event still work.My question is why the next
                    > video:
                    > http://link.brightcove.com/services/player/bcpid1681730258?bctid=1606789346 ,
                    > She needs to change the code by passing event into populateMailAddress,
                    > and
                    > then she never used the variable event.
                    >
                    > I have c++, java programming experience. From my understanding, if the
                    > function does not need the variable, then we don't need to pass that
                    > variable
                    > into the function. We only pass the variables that the function NEEDS
                    > them.

                    Except that when you use addEventListener, there is no syntax to say and by
                    the way don't pass in the event. Rather than arguing with everyone, just
                    take the example in this video
                    http://link.brightcove.com/services/player/bcpid1681730258?bctid=1606789346
                    and change this:

                    private function populateMailingAddress(event:MouseEvent)

                    to this:

                    private function populateMailingAddress()

                    and run the code. You should then have less difficulty understanding what
                    people have been trying to tell you.

                    HTH;

                    Amy


                    • 7. Question on event object
                      cpthk Level 1
                      Sorry, guys. I probably brought out even more confusion here.
                      I will ask in this way, I created a temp. flex file.
                      Here is the code:
                      /********************************/
                      <?xml version="1.0" encoding="utf-8"?>
                      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">

                      <mx:Script>
                      <![CDATA[

                      private function populateMailingAddress(event:MouseEvent):void {
                      mailingAddress.text = homeAddress.text;
                      }
                      ]]>
                      </mx:Script>
                      <mx:TextInput id="homeAddress" x="22" y="167"/>
                      <mx:TextInput id="mailingAddress" x="247" y="167"/>
                      <mx:CheckBox x="247" y="209" label="Checkbox" click="populateMailingAddress(event)"/>
                      </mx:Application>
                      /*****************************/

                      If I changed to:
                      /********************************/
                      <?xml version="1.0" encoding="utf-8"?>
                      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">

                      <mx:Script>
                      <![CDATA[

                      private function populateMailingAddress():void {
                      mailingAddress.text = homeAddress.text;
                      }
                      ]]>
                      </mx:Script>
                      <mx:TextInput id="homeAddress" x="22" y="167"/>
                      <mx:TextInput id="mailingAddress" x="247" y="167"/>
                      <mx:CheckBox x="247" y="209" label="Checkbox" click="populateMailingAddress()"/>
                      </mx:Application>
                      /*****************************/

                      I removed the event object which originally passed into the function. But when I run both of them, I see no difference. Both codes could copy the left text to the right. This is my question.
                      In this video: http://link.brightcove.com/services/player/bcpid1681730258?bctid=1606789339 , at the first half of the video. Please look at time 1:53 of the video. She basically just added event code and passed into the function. Which is the same as what I did to my code. Then what is the benefit of passing the event object? This is actually my question is. She also mentioned passing the event object is OPTIONAL. This is where I got confused at, since without passing the event object still works, then what makes any difference when passing the event object.
                      • 8. Re: Question on event object
                        Level 7
                        In that situation, you don't need to use the event. The event was used
                        in that video for a couple of reasons. First of all, she wanted to show
                        that you could access the event if you needed to, and how to do it.
                        Second, she wanted to use it in conjunction with a breakpoint to show
                        what was contained in the event listener. Third, she wanted to set up
                        her code for the other video that you posted.

                        http://link.brightcove.com/services/player/bcpid1681730258?bctid=1606789346

                        In this video, she uses stops using the click="myFunction(event)" style
                        handler, and uses addEventListener in order to trigger the function call
                        when the event is dispatched. When you use addEventListener, the
                        function you pass it MUST take the event as an argument, for the reasons
                        I explained earlier.
                        • 9. Re: Question on event object
                          Level 7

                          "cpthk" <webforumsuser@macromedia.com> wrote in message
                          news:gndtit$gqq$1@forums.macromedia.com...
                          > Sorry, guys. I probably brought out even more confusion here.
                          > I will ask in this way, I created a temp. flex file.
                          > Here is the code:
                          > /********************************/
                          > <?xml version="1.0" encoding="utf-8"?>
                          > <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
                          > layout="absolute">
                          >
                          > <mx:Script>
                          > <![CDATA[
                          >
                          > private function populateMailingAddress(event:MouseEvent):void {
                          > mailingAddress.text = homeAddress.text;
                          > }
                          > ]]>
                          > </mx:Script>
                          > <mx:TextInput id="homeAddress" x="22" y="167"/>
                          > <mx:TextInput id="mailingAddress" x="247" y="167"/>
                          > <mx:CheckBox x="247" y="209" label="Checkbox"
                          > click="populateMailingAddress(event)"/>
                          > </mx:Application>
                          > /*****************************/
                          >
                          > If I changed to:
                          > /********************************/
                          > <?xml version="1.0" encoding="utf-8"?>
                          > <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
                          > layout="absolute">
                          >
                          > <mx:Script>
                          > <![CDATA[
                          >
                          > private function populateMailingAddress():void {
                          > mailingAddress.text = homeAddress.text;
                          > }
                          > ]]>
                          > </mx:Script>
                          > <mx:TextInput id="homeAddress" x="22" y="167"/>
                          > <mx:TextInput id="mailingAddress" x="247" y="167"/>
                          > <mx:CheckBox x="247" y="209" label="Checkbox"
                          > click="populateMailingAddress()"/>
                          > </mx:Application>
                          > /*****************************/
                          >
                          > I removed the event object which originally passed into the function. But
                          > when
                          > I run both of them, I see no difference. Both codes could copy the left
                          > text to
                          > the right. This is my question.

                          The way you have it above, if you had the event code in there, you'd get an
                          error because you are NOT passing an event. You can call a handler for an
                          event from MXML without passing the event object in. However, if you added
                          the event listener in ActionScript, as was shown in the example you posted
                          http://link.brightcove.com/services/player/bcpid1681730258?bctid=1606789346

                          Then when you tried to call the function, you'd get an error.

                          You seem to have an intuitive understanding of what's happening here without
                          knowing that you understand, because you're actually avoiding causing the
                          errors that would be caused by trying to add the event listener in
                          actionscript and have a handler that doesn't have an argument for it. So I
                          say just go with your gut and that should get you by fine until you get into
                          the situation where you _do_ get the errors we've been telling you about.
                          And then you'll go "aha! That was what they meant!" and you'll figure it
                          out.