4 Replies Latest reply on Aug 4, 2009 4:57 PM by Gregory Lafrance

    How to use dataprovider for my own custom component?

    webvalue Level 1

      Hi,

       

      I made a component to display a set of data, 9 text fields. I would like to use data provider to fill the 9 fields all at once and also syncronize the dataprovider to the current values of the text fields because the user may change them.

       

      I have never seen this kind of thing before. Is it possible? If so, are there any examples or documents?

       

      Thanks.

        • 1. Re: How to use dataprovider for my own custom component?
          Madhav Subedi Level 4

          for this, use an ARRAY of strings to fill each textinput.

          tf1.text = arr[0];

          .

          .

          .

          .

          declare the array BINDABLE.

          and specify getter/setter for the arr variable, if you cant, then just make it public.

           

          to synchronize the array,

          set the related index value to text property of textinput with catching the change event of textfield.

          • 2. Re: How to use dataprovider for my own custom component?
            tanyagray01
            <!-- your main app -->
            <mx:Script>
                 [Bindable] private var dataArray:Array = ["text field one", "text for field two", "this goes in three"];
            </mx:Script>
            
            <custom:MyComponent dataProvider="{dataArray}" />
            

             

             

            <!-- your component -->
            <mx:VBox>
                 <mx:Script>
                           <![CDATA[
                           [Bindable] private var _textFieldData:Array;
            
                           public function set dataProvider(dataArray:Array):void{
                                _textFieldData = dataArray; // when this component receives its dataprovider, save it to local variable
                           }
            
                           public function get dataProvider():Array{
                                return _textFieldData;
                           }
            
                           ]]>
                 </mx:Script>
            
            
                 <mx:TextInput id="text0" text="{_textFieldData[0]}" change="{_textFieldData[0] = text0.text}"/>
                 <mx:TextInput id="text1" text="{_textFieldData[1]}" change="{_textFieldData[1] = text1.text}"/>
                 <mx:TextInput id="text2" text="{_textFieldData[2]}" change="{_textFieldData[2] = text2.text}"/>
            
            </mx:VBox>
            

             

            This is exactly what Madhav said but it's the code for it. May be mistakes in it, sorry, but you get the idea.

            • 3. Re: How to use dataprovider for my own custom component?
              webvalue Level 1

              Thanks a lot for the help. However, I need to use AS to do the binding and now the array is a big pubble to me to do the binding.

               

              None of the below is working:

               

              BindingUtils.bindProperty(text0, "text", this, _textFieldData[0]);

              BindingUtils.bindProperty(text0, "text", this, _textFieldData[0]);

              BindingUtils.bindProperty(text0, "text", _textFieldData[0], "value");

               

               

              Any help is much appreciated.

              • 4. Re: How to use dataprovider for my own custom component?
                Gregory Lafrance Level 6

                You might be able to use the TextField change event and the ArrayCollection collectionChange events to implement this type of "binding".

                 

                So when any of the TextFields text changes, call a function that updates the ArrayCollection from all the TextFields, and when the ArrayCollection dispatches the collectionChange event, update the TextFields.

                 

                This actually might not work, because you could end up with a endless loop, but it might be possible, especially if you use an intermediate temp ArrayCollection.

                 

                If this post does happen to answer your question or helps, please mark it as such.