6 Replies Latest reply on Feb 15, 2010 12:42 AM by Francisc

    Controling Binding

    Francisc Level 3

      Hello!

       

      How can I stop bindingin one direction but keep it in the other?

       

      Take this example:

       

      [Bindable] pubilc var AC1:ArrayCollection=new ArrayCollection([CONSIDER THIS POPULATED]);

      [Bindable] pubilc var AC2:ArrayCollection;

       

      private function init():void

      {

           AC2=AC1;

           AC2.filterFunction=someFunction;

           AC2.refresh();

      }

      (...)

      <mx:DataGrid dataProvider="{AC2}">

      (...)

       

      Q: How can I prevent the filtering on AC2 reflect on AC1, but still keep data from AC1 flowing into AC1?

      Note: AC2 needs to be bindable so that the DataGrid reflects its data.

       

      Thank you!

        • 1. Re: Controling Binding
          archemedia Level 4

          nasty one

           

          You cannot use AC2 = AC1, that's for sure.

           

          A few ideas maybe:

           

          - Make a custom ArrayCollection class within it a second ArrayCollection (nestedAC) that holds a copy of the original one. You can use several Array function on the source property  to do this.

          - Override some ArrayCollection function (like AddItem..) to keep track of the nested one.

          - Set the filterfunction to AC1.nestedAC

          - Bind DG1 to AC1 and DG2 to AC1.nestedAC

           

          Just an idea

           

          Dany

          1 person found this helpful
          • 2. Re: Controling Binding
            Francisc Level 3

            Yes, it's nasty indeed.

             

            The problem is that I want to reflect changes to AC2...

             

            What if I were to use BindUtils to bind AC1 to AC2 and AC2 to the DataGrid?

             

            Would that make it one way?

            • 3. Re: Controling Binding
              archemedia Level 4

              Using bindingUtils to bind in one way might be a good idea too.

              In my scenario, AC2 will reflect changes in AC1.

               

              If you override the addItem function, you can add the item to nestedAC so this will reflect the changes!

               

              If you post a dummy app, I will do some testing too if you want.

               

              Dany

              • 4. Re: Controling Binding
                Francisc Level 3

                Hm, I don't have a specific app in mind, I was just wondering how to do this as I have had problems with this in the past.

                 

                I think I told you this before, but what I do to stop this is:

                 

                //Inside a function

                var acTemp:ArrayCollection=AC1;

                for(var i:uint=0;i<acTemp.length;i++)

                {

                     AC2.addItem(acTemp[i]);

                }

                acTemp=null;

                 

                AC2 stops sending changes to AC1.

                The only problem with this is that if you want to reflect changes in AC1 you have to call that function again.

                • 5. Re: Controling Binding
                  archemedia Level 4

                  Hey, check this:

                   

                  <?xml version="1.0" encoding="utf-8"?>
                  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="init()">
                     
                      <mx:Script>
                          <![CDATA[
                              import mx.collections.ArrayCollection;
                             
                              [Bindable]
                              private var AC1:ArrayCollection;
                             
                              private function init():void
                              {
                                  var a1:Array = [{name: "Aaron"}, {name: "Abe"}, {name: "Caroline"}];
                                  AC1 = new ArrayCollection();
                                  AC1.source = a1;
                                  AC1.refresh();
                              }
                             
                              private function filter(item:Object):Boolean
                              {
                                  return (item.name.charAt() == "A");
                              }
                          ]]>
                      </mx:Script>
                      <mx:ArrayCollection id="AC2" source="{AC1.source}" filterFunction="filter"/>
                      <mx:Button label="add" click="AC1.addItem({name: 'Android'});AC1.refresh();AC2.refresh()"/>
                     
                      <mx:List dataProvider="{AC1}" labelField="name"/>
                      <mx:List dataProvider="{AC2}" labelField="name"/>
                  </mx:Application>

                  • 6. Re: Controling Binding
                    Francisc Level 3

                    Sorry for the delay, I went away for the week-end.

                    I'll give it a test as soon as my laptop is fixed.

                     

                    Thanks!