10 Replies Latest reply on Oct 1, 2008 11:08 AM by Newsgroup_User

    "this"

    Woxorz
      Hello, I am new to flexing and actionscripting and I hoped some of you could advise me on "this" problem.
      I want my MXML to look something like this:

      <mx:Button x="177" y="140" label="Button" click="labelChange(this, "New Label");"/>

      While the actionscript will look something like this:

      public function labelChange(btn, newLabel):void
      {
      btn.label = "hello";
      }

      I want to pass "this" as a parameter to a function so that it will reference the button object inside the function. This works fine in javascript but it seems to do nothing in flex. I mean it compiles, but there is no visible result. I can give the button an id and reference it that way but then I would have to write a different stupid function for each button. :(

      What am I doing wrong?

        • 1. Re: &quot;this&quot;
          flex2008 Level 3
          I dont know wat exactly is the problem in this case.but i can suggest u an alternate way
          <mx:Button id="btn1" x="177" y="140" label="Button" click="labelChange(event)"/>

          public function labelChange(e:Event):void
          {
          e.currentTarget.label="hello";
          }

          As far as ur eg is concerned,i think the problem is u haven defined any datatype for btn in the function declaration
          • 2. Re: &quot;this&quot;
            Level 7

            "flex2008" <webforumsuser@macromedia.com> wrote in message
            news:gbsf09$l8l$1@forums.macromedia.com...
            >I dont know wat exactly is the problem in this case.but i can suggest u an
            > alternate way
            > <mx:Button id="btn1" x="177" y="140" label="Button"
            > click="labelChange(event)"/>
            >
            > public function labelChange(e:Event):void
            > {
            > e.currentTarget.label="hello";
            > }
            >
            > As far as ur eg is concerned,i think the problem is u haven defined any
            > datatype for btn in the function declaration

            What e.mail client are you using? Your spell checker sucks!


            • 3. Re: &quot;this&quot;
              Level 7

              "Woxorz" <webforumsuser@macromedia.com> wrote in message
              news:gbscn9$iks$1@forums.macromedia.com...
              > Hello, I am new to flexing and actionscripting and I hoped some of you
              > could
              > advise me on "this" problem.
              > I want my MXML to look something like this:
              >
              > <mx:Button x="177" y="140" label="Button" click="labelChange(this, "New
              > Label");"/>
              >
              > While the actionscript will look something like this:
              >
              > public function labelChange(btn, newLabel):void
              > {
              > btn.label = "hello";
              > }
              >
              > I want to pass "this" as a parameter to a function so that it will
              > reference
              > the button object inside the function. This works fine in javascript but
              > it
              > seems to do nothing in flex. I mean it compiles, but there is no visible
              > result. I can give the button an id and reference it that way but then I
              > would
              > have to write a different stupid function for each button. :(
              >
              > What am I doing wrong?

              All properties inside a class by default refer to that class:

              public function labelChange(newLabel:String){
              label=newLabel;
              }


              • 4. Re: &quot;this&quot;
                ntsiii Level 3
                "this" refers to the current scope, which is essentially the current component instance. Actionscript is different than javascript.

                And to do what you are trying to do, give your button an "id", and pass that:

                <mx:Button id="btn1" ... click="labelChange(btn1, "New Label");"/>

                But, despite the linguistic mush, flex 2008's suggestion is probably the better way.

                Tracy
                • 5. Re: &quot;this&quot;
                  Woxorz Level 1
                  Hmmm, that clears things up a bit. I am considering using flex 2008's approach.

                  ntsiii, if I passed the id as a parameter, what would the function itself look like? How do I tell actionscript that this references the id of a button?

                  getElementById? haha
                  • 6. Re: &quot;this&quot;
                    istrasci
                    quote:

                    Originally posted by: Woxorz
                    Hmmm, that clears things up a bit. I am considering using flex 2008's approach.

                    ntsiii, if I passed the id as a parameter, what would the function itself look like? How do I tell actionscript that this references the id of a button?

                    getElementById? haha



                    Just so you don't have to wait for one person to reply... The function would just look like

                    labelChange(btn:Button, newLabel:String):void { btn.label = newLabel; }
                    • 7. Re: &quot;this&quot;
                      Woxorz Level 1
                      That's terrific. Thanks y'all for your help!
                      • 8. Re: &quot;this&quot;
                        Level 7

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

                        Originally posted by: Woxorz
                        > Hmmm, that clears things up a bit. I am considering using flex 2008's
                        > approach.
                        >
                        > ntsiii, if I passed the id as a parameter, what would the function itself
                        > look
                        > like? How do I tell actionscript that this references the id of a button?
                        >
                        > getElementById? haha

                        >
                        >
                        > Just so you don't have to wait for one person to reply... The function
                        > would
                        > just look like
                        >
                        > labelChange(btn:Button, newLabel:String):void { btn.label = newLabel; }

                        Since the button is actually "this" according to the OP, there's no need to
                        pass a reference to the button.


                        • 9. Re: &quot;this&quot;
                          istrasci Level 1
                          quote:

                          Originally posted by: Newsgroup User
                          Since the button is actually "this" according to the OP, there's no need to
                          pass a reference to the button.


                          The button shouldn't be "this"... If the situation is as follows,

                          <component>
                          <mx:Button id="b1" click="cH()"... />
                          <mx:Script>
                          ... private function cH():void { ***** }
                          </mx:Script>
                          </component>

                          , then any use of "this" inside the function cH will refer to the instantiation of 'component', not the button.

                          The way I understood the original post, the button is a child of some component (as I described above), not a subclass of mx:Button... It seems like s/he wanted to use a reference to the button inside the function (thinking the "this" inside the <mx:Button> tag would refer to the button but finding out otherwise). Your way will work if the handler is only used for that one button, but it seemed like s/he possibly wanted to use the same handler for various buttons. In that case, you'd have to know which button called it, so you'd either need to use my suggestion or use the event target method.

                          Maybe OP can clarify the intended use.
                          • 10. Re: &quot;this&quot;
                            Level 7

                            "istrasci" <webforumsuser@macromedia.com> wrote in message
                            news:gc0dot$49n$1@forums.macromedia.com...
                            >
                            quote:

                            Originally posted by: Newsgroup User
                            > Since the button is actually "this" according to the OP, there's no need
                            > to
                            > pass a reference to the button.

                            >
                            > The button shouldn't be "this"... If the situation is as follows,
                            >
                            > <component>
                            > <mx:Button id="b1" click="cH()"... />
                            > <mx:Script>
                            > ... private function cH():void { ***** }
                            > </mx:Script>
                            > </component>
                            >
                            > , then any use of "this" inside the function cH will refer to the
                            > instantiation of 'component', not the button.
                            >
                            > The way I understood the original post, the button is a child of some
                            > component (as I described above), not a subclass of mx:Button... It seems
                            > like
                            > s/he wanted to use a reference to the button inside the function (thinking
                            > the
                            > "this" inside the <mx:Button> tag would refer to the button but finding
                            > out
                            > otherwise). Your way will work if the handler is only used for that one
                            > button, but it seemed like s/he possibly wanted to use the same handler
                            > for
                            > various buttons. In that case, you'd have to know which button called it,
                            > so
                            > you'd either need to use my suggestion or use the event target method.

                            You're right, but I interpreted it as a subclass of button. I sometimes
                            have a hard time figuring out what the knowledge level/viewpoint is of the
                            person posting :-)