1 Reply Latest reply on Jul 31, 2008 8:28 AM by Newsgroup_User

    bidirectional binding

    leilingdeng
      // ------- myVO.as -----------
      package {
      public class myVO {
      public var name:String;
      public var phone:String;
      }

      // ------ SampleForm.mxml ---------
      <mx:Script>
      <![CDATA[
      [Bindable]
      public var vo:myVO= new myVO();
      ]]>
      </mx:Script>

      <mx:Form>
      <mx:FormItem label="Name">
      <mx:TextInput id="nameTextInput" text="{ vo.name}" />
      </mx:FormItem>
      <mx:FormItem label="Phone">
      <mx:TextInput id="phoneTextInput" text="{vo.phone}" />
      </mx:FormItem>
      </mx:Form>
      // --------- here's how to create two way binding ------------
      <mx:Binding source="nameTextInput.text" destination="vo.name" />
      <mx:Binding source="phoneTextInput.text" desitnation="vo.phone" />


      I find myself wanting to do bidirectional binding, but I have the
      feeling there may be some drawbacks to this.

      I want a screen's controls to be initialized (bound to) a data model.
      As the user changes the controls' values, I want the data model to
      reflect this. In this case the data model is basically a VO. Is
      there a best practice for this?

      thanks
        • 1. Re: bidirectional binding
          Level 7

          "leilingdeng" <webforumsuser@macromedia.com> wrote in message
          news:g6rtkc$8g8$1@forums.macromedia.com...
          > // ------- myVO.as -----------
          > package {
          > public class myVO {
          > public var name:String;
          > public var phone:String;
          > }
          >
          > // ------ SampleForm.mxml ---------
          > <mx:Script>
          > <![CDATA[
          > [Bindable]
          > public var vo:myVO= new myVO();
          > ]]>
          > </mx:Script>
          >
          > <mx:Form>
          > <mx:FormItem label="Name">
          > <mx:TextInput id="nameTextInput" text="{ vo.name}" />
          > </mx:FormItem>
          > <mx:FormItem label="Phone">
          > <mx:TextInput id="phoneTextInput" text="{vo.phone}" />
          > </mx:FormItem>
          > </mx:Form>
          > // --------- here's how to create two way binding ------------
          > <mx:Binding source="nameTextInput.text" destination="vo.name" />
          > <mx:Binding source="phoneTextInput.text" desitnation="vo.phone" />
          >
          >
          > I find myself wanting to do bidirectional binding, but I have the
          > feeling there may be some drawbacks to this.
          >
          > I want a screen's controls to be initialized (bound to) a data model.
          > As the user changes the controls' values, I want the data model to
          > reflect this. In this case the data model is basically a VO. Is
          > there a best practice for this?

          If you need to validate things, it is better to assign the data back to the
          data model once it has been validated. If you bind directly, you can't do
          this.

          HTH;

          Amy