topical media & game development

talk show tell print

professional-flex-code-14-BindWithGettersAndSetters.mx

professional-flex-code-14-BindWithGettersAndSetters.mx [swf] flex


  <?xml version="1.0" encoding="utf-8"?>
  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    backgroundColor="#FFFFFF">
     
    <mx:Script>
      <![CDATA[
      private var _phoneNumeber:String = "";
      // Bind getter function to phoneNumberChanged event
      [Bindable(event="phoneNumberChanged")]
      public function get phoneNumber():String {
        return _phoneNumeber;
      }
      // Setter method.
      public function set phoneNumber(value:String):void {
        if (value.length<10) {
          _phoneNumeber = value;
        } else {
          _phoneNumeber = phoneFormatter.format(value);
        }
        // Create and dispatch event 
        var eventObj:Event = new Event("phoneNumberChanged");
        dispatchEvent(eventObj);
      }       
      ]]>
    </mx:Script>
    <mx:PhoneFormatter id="phoneFormatter" 
      formatString="( 
)

-

" validPatternChars="#-() "/>



<mx:Panel title="Bind with Getters and Setters" width="500" height="90" paddingTop="10" paddingLeft="10" paddingRight="10" paddingBottom="10" layout="horizontal"> <mx:TextInput id="ti1" change="phoneNumber=ti1.text" maxChars="10" restrict="0-9"/> <mx:TextInput id="ti2" text="{phoneNumber}"/> </mx:Panel> </mx:Application>


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.