3 Replies Latest reply on Feb 24, 2008 11:16 AM by VarioPegged

    newbie: binding actionscript var to textinput

    andk
      Hi,

      I'm just starting with flex, and have a simple question on bindings. I've created the following application (using flex3 beta 3, and flexbuilder):

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">
      <mx:Script>
      <![CDATA[
      [Bindable]
      public var customerName:String = "customerName";
      ]]>
      </mx:Script>
      <mx:TextInput text="{customerName}" x="187" y="92"/>
      <mx:Text text="{customerName}" x="368" y="94"/>
      <mx:Button click="customerName += 'z'" x="473" y="92" label="add z"/>
      </mx:Application>

      The problem: the variable customerName isn't changed when i enter some text in the textinput. Pressing the button does the expected thing: changes the variable, which is displayed correctly by the textinput and text components.

      My goal is to create a datamodel using an actionscript object. I've seen some examples where the datamodel is bound to the textinput (the other way around), but it seems to me that it would be better not to tie a datamodel to one single input component. I may want to modify the data using multiple components.

      Thanks,
      Andrej
        • 1. Re: newbie: binding actionscript var to textinput
          tschorakl
          Hi,

          why are you doing it not so

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">
          <mx:Script>
          <![CDATA[
          [Bindable]
          public var customerName:String = "customerName";
          ]]>
          </mx:Script>
          <mx:TextInput id="txtI" text="{customerName}" keyUp="{customerName = txtI.text}" x="187" y="92"/>
          <mx:Text text="{customerName}" x="368" y="94"/>
          <mx:Button click="customerName += 'z'" x="473" y="92" label="add z"/>
          </mx:Application>

          regards, Tom
          • 2. Re: newbie: binding actionscript var to textinput
            andk Level 1
            I thought that wasn't necessary when using bindings.

            Btw, i solved the problem using the binding component:

            <mx:TextInput id="customerNameField" x="187" y="92"/>
            <mx:Binding source="customerNameField.text" destination="customerName"/>
            <mx:Binding destination="customerNameField.text" source="customerName"/>

            Is this the best way to solve it using bindings?

            Thanks,
            Andrej
            • 3. Re: newbie: binding actionscript var to textinput
              VarioPegged Level 2
              My 2 cents: There is often a misconception that when making a variable bindable, it is a two-way binding, in other words, the entity that is bound to the variable will be updated automatically AND the variable will be updated if the entity's value changes (a circular binding).

              Making your variable [Bindable] means that there is a single observer, like your TextInput, of a variable, like customerName. If the customerName variable changes, then the TI will change, not the other way around.

              Your approach of using the Binding tag with a reciprocal source and destination should be fine.

              My approach usually is to set the initial value I want displayed in the TI from elsewhere. Then the TI is strictly used as the control to input data in order to update a variable on the 'change' event. I often bind to a function that'll automatically perform XYZ for me on a specific event.

              TS