4 Replies Latest reply on Aug 20, 2010 7:52 AM by ratnawe

    Access hostComponent property from SparkSkin (public var vs getter/setter)

    ratnawe Level 1

      Hey Flexers,

       

      Any ideas on that?

       

      I have written a custom component derived from a Spark TextInput.

      I have written a custom skin derived from SparkSkin for this custom component.

       

      The custom component has a property called "prompt" and from my custom skin I want to access this property via "hostComponent.prompt". Now I thought I have two equivalent options to implement this property:

       

      1) simple public variable:

       

      [Bindable] public var prompt:String;

       

      2) private variable with getters and setters:

       

      [Bindable] private var _prompt:String;

       

      public function get prompt():String
      {
          return _prompt;
      }

      public function set prompt(value:String):void
      {
          _prompt= value;
      }

       

      If I use solution 1 it works.

      If I use solution 2 it doesn't work. The compiler will warn me with "Data binding will not be able to detect assignments to 'prompt'." And this warning infact is correct so that assignments to the variable will not be recognized by the skin.

       

      But why?

       

      Shouldn't these two solutions be equivalent?

       

      It just cost me a lot of time to find out, that's why I'm asking, hoping to learn the answer

       

      Thanks for any comments and answers!

       

       

       

      tags: flex 4, SparkSkin, hostComponent, data binding

        • 1. Re: Access hostComponent property from SparkSkin (public var vs getter/setter)
          cjolif-Fc6gNO

          You probably have put at the wrong place for 2.

           

          The following should work:

           

          private var _prompt:String;

           

           

          public function get prompt():String

          {

              return _prompt;

          }

           

          public function set prompt(value:String):void

          {

              _prompt= value;

          }

           

          Christophe Jolif

          ILOG Elixir Lead

           

           

           

           

           

          From:

          ratnawe <forums@adobe.com>

          To:

          Christophe Jolif/France/IBM@IBMFR

          Date:

          08/20/2010 03:07 PM

          Subject:

          Access hostComponent property from SparkSkin (public var vs

          getter/setter)

           

           

           

           

          Hey Flexers,

           

          Any ideas on that?

           

          I have written a custom component derived from a Spark TextInput.

          I have written a custom skin derived from SparkSkin for this custom

          component.

           

          The custom component has a property called "prompt" and from my custom

          skin I want to access this property via "hostComponent.prompt". Now I

          thought I have two equivalent options to implement this property:

           

          1) simple public variable:

           

          public var prompt:String;

           

          2) private variable with getters and setters:

           

          private var _prompt:String;

           

          public function get prompt():String

          {

              return _prompt;

          }

           

          public function set prompt(value:String):void

          {

              _prompt= value;

          }

           

          If I use solution 1 it works.

          If I use solution 2 it doesn't work. The compiler will warn me with "Data

          binding will not be able to detect assignments to 'prompt'." And this

          warning infact is correct so that assignments to the variable will not be

          recognized by the skin.

           

          But why?

           

          Shouldn't these two solutions be equivalent?

           

          It just cost me a lot of time to find out, that's why I'm asking, hoping

          to learn the answer

           

          Thanks for any comments and answers!

           

           

           

          tags: flex 4, SparkSkin, hostComponent, data binding

          1 person found this helpful
          • 2. Re: Access hostComponent property from SparkSkin (public var vs getter/setter)
            UbuntuPenguin Level 4

            Shouldn't these two solutions be equivalent? ...

             

            No , they shouldn't be equivalent because they aren't. When you are using getters and setters , you have to dispatch an event in the setter ( or any function that changes the result of the getter ) for it to be bindable.

             

            These two things are equivalent.

             

            [Bindable] public var prompt:String;

             

            and

             

            private var _prompt:String;

             

            [Bindable(Event="promptChange")]

            public function get prompt():String

            {

              return _prompt;

            }

             

            public function set prompt(  value:String  ):void

            {

              _prompt = value;

              dispatchEvent("promptChange");

            }

             

            If this post was helpful , please mark it as such.

            1 person found this helpful
            • 3. Re: Access hostComponent property from SparkSkin (public var vs getter/setter)
              ratnawe Level 1

              Your post was very helpful. Helped me to better understand how data binding works. Thanks a lot!

              • 4. Re: Access hostComponent property from SparkSkin (public var vs getter/setter)
                ratnawe Level 1

                Thanks for pointing that out. It actually worked that way!