2 Replies Latest reply on May 25, 2009 5:09 AM by yancaoshi

    One question about data binding

    yancaoshi Level 1

      Hi all,

       

      Data binding is easy to use when developing. I often define new property with public directive, but I find I can't control the processing when property value changes. I learned that there's a propertyChange event when the bindable property changes. If a property is set to 29, and I want it to be ceiled to 30 in the background. How can I achieve this?

       

      Thanks,

        • 1. Re: One question about data binding
          BennyBeta Level 4

          Hi yancaoshi.  I'm not sure if I understand the question, so if I miss just let me know.

           

          You can change the value a property is set to when it is set using get/set functions.

           

          private var _value:Number = 0;

           

          [Bindable]

          public function set value( v:Number ):void

          {

               _value = v;

          }


          public function get value():Number

          {

               return _value;

          }

           

          A regular get/set setup looks something like the above code.  It creates a property called "value" that can be accessed just as if it were declared as a public var.  The difference is that you can add some logic to the set function.

           

          [Bindable]

           

          public function set value( v:Number ):void

          {

               // silly logic to raise 29 to 30

               if( v == 29 ) {

                    _value = 30

               } else {

                    _value = v;

               }

          }

           

          Now when someone calls the below code, the value of the "value" property is actually set to 30 and anyone "binded" to that property will get the value 30.

           

           

          The following documentation may prove helpful:

          http://www.flexafterdark.com/docs/ActionScript-GetSetFunctions

          http://www.flexafterdark.com/docs/Flex-Binding

           

          Let me know if that helps at all.

           

          Ben Edwards

          • 2. Re: One question about data binding
            yancaoshi Level 1

            Hi Edwards,

             

            Your answer is very helpful. But is it possible to override propertyChange method, and give me an example as well.

             

            Best wishes,

            yancaoshi