3 Replies Latest reply on Apr 30, 2009 7:17 AM by Paul Reilly

    Simple Data Binding syntax question

    jeffreypritchard

      Hi,

       

      Is there an mxml equivalent of following AS3 syntax:

       

      dataObject[dataField].

       

      The reason I am asking is because I am trying to bind the text property of a TextInput component to an Object's properties. I'm basically looping through the properties of an object, and trying to bind the object's property value to the text field of the TextInput component.

       

      Here's the code:

              <mx:TextInput  id="txtValue"  text="{dataSource[dataField]}"/>

       

      The compiler throws a warning saying:

      "Data binding will not be able to detect changes when using square bracket operator.  For Array, please use ArrayCollection.getItemAt() instead."

       

      I'm really stuck, Thanks in advance for your time.

        • 1. Re: Simple Data Binding syntax question
          Gregory Lafrance Level 6

          I would use XML data and then use e4x syntax to do this.

           

          Maybe give more code and sample data so we can play with this.

          • 2. Re: Simple Data Binding syntax question
            jeffreypritchard Level 1

            I created a component which contains a TextInput field and a Button. The dataField property is set in the mxml declaration.

             

            Here's the nuts and bolts of the component:

             

            <mx:FormItem  xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="initializeField()">

             

                <mx:Script>
                    <![CDATA[

                        import com.controller.Service;

                        import com.model.vo.UserVO;

                        import com.model.Model;


                       [Bindable]public var dataSource:UserVO

                       [Bindable]public var dataField:String = null;
                       
                        private function initializeField():void{
                            this.label = '[' + dataField + ']'
                        }

             

                        private function getData(e:MouseEvent):void{

                            // make sure the user is not null

                            if (Model.getInstance().CurrentUser != null){


                                dataSource = Model.getInstance().CurrentUser;

                                Service.getUserProperty(dataSource.UserID,dataField);


                            }

                        }
                       
                    ]]>
                </mx:Script>

             

                <mx:HBox>

                    <mx:TextInput   id="txtValue" text="{dataSource.[dataField]}"/>

                    <mx:Button label="GET" click="getData(event)"/>                               
                </mx:HBox>

             

            </mx:FormItem>

             

            The dynamic method calls work, but I just can't get the text property of the TextInput to bind to the data.

             

            Thanks

            • 3. Re: Simple Data Binding syntax question
              Paul Reilly Level 1

              Data binding doesn't handle square bracket notion very well, because the compiler assumes that it's used to access an Array or an Object, neither of which has support for dispatching change events when an element or a property is changed.  Putting Bindable metadata on the variable just means that a change event is dispatched when the variable is updated, not when the contents of the variable is updated. You have lots of options, though.  If you control the UserVO class, then you could add an accessor function, something like getProperty() and in your data binding expression use dataSource.getProperty(dataField).  If you don't control UserVO, then you could add a local utility function and then do something like getProperty(dataSource, dataField).  In both cases, data binding will be smart enough to fire the data binding if dataSource or dataField changes.

              1 person found this helpful