1 Reply Latest reply on May 30, 2009 5:28 PM by K-kOo

    Why this Binding is working ?

    K-kOo

      Hi everybody, i though i was trough with binding but after an another simple test, i dont know why this one worked. The final result of the code below is displaying 2 list and 2 datagrid. Each of them link to either an Array either an ArrayCollection. Using 2 different method of how the dataprovider is linked to them. The data in the arrays are change periodically with a timer.

      Final result : any of the above combinations is resulting in a good refresh of the display.

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
      <mx:Script>
         <![CDATA[
            import flash.utils.setInterval;
            import flash.utils.setTimeout;
            import mx.utils.ObjectProxy;
            import mx.collections.ArrayCollection;
            import mx.collections.ICollectionView;
            
            public var vertices1:ICollectionView = new ArrayCollection
            ([
               new MyCoord(1.0, 1.0),
               new MyCoord2(1.0, 1.0),
               new ObjectProxy({x:1.0, y:1.0}),
               {x:1.0, y:1.0}
            ]);
            
            public var vertices2:Array =
            [
               new MyCoord(1.0, 1.0),
               new MyCoord2(1.0, 1.0),
               new ObjectProxy({x:1.0, y:1.0}),
               {x:1.0, y:1.0}
            ];
            
            private function changesVertices():void
            {
               var allVertices:Array = [vertices1, vertices2];
               for each (var vertices in allVertices)
                  for each (var v:Object in vertices)
                  {
                     v.x += Math.random() * 2 * (Math.random() < 0.5 ? 1 : -1);
                     v.y += Math.random() * 2 * (Math.random() < 0.5 ? 1 : -1);
                     v.x = format(v.x, 2);
                     v.y = format(v.y, 2);
                  }
            }
            
            private function format(num:Number, precision:Number):Number
            {
               var dec:Number = Math.pow(10, precision);
               return Math.round(num * dec) / dec;
            }
         ]]>
      </mx:Script>
      <mx:creationComplete>
         <![CDATA[
            var interval:uint = setInterval(changesVertices, 1000);
            list.dataProvider = vertices1;
            dg.dataProvider = vertices2;
         ]]>
      </mx:creationComplete>
      <mx:HBox paddingBottom="10" paddingLeft="10" paddingRight="10" paddingTop="10" width="100%" height="50%">
         <mx:List id="list" width="50%"/>
         <mx:DataGrid id="dg" width="50%"/>
      </mx:HBox>
      <mx:HBox paddingBottom="10" paddingLeft="10" paddingRight="10" paddingTop="10" width="100%" height="50%">
         <mx:List id="list2" width="50%" dataProvider="{vertices1}"/>
         <mx:DataGrid id="dg2" width="50%" dataProvider="{vertices2}"/>
      </mx:HBox>
      </mx:WindowedApplication>
      
      

       

      MyPoint MyPoint2 are simple classes as you can see with just 2 attributes x and y. The difference between the two is that in MyPoint x and y are Bindable whereas none of them are in MyPoint2. I also used an Object (so no binding into it) and an ObjectProxy (so with a binding system)

       

      Also, i used a simple Array as opposed to a ArrayCollection thinking that it was maybe the collection doing more job than expected.

       

      And finally, i thought that maybe direclty assigning the array to the dataprovider would have shorten the link between the data and the display but even with the classic curly brackets syntax it's working.

       

      Can someone explain this to me ?

        • 1. Re: Why this Binding is working ?
          K-kOo Level 1

          Hehe, it's always like that. You're asking your question and it's at this time that found the explanation

           

          The reason why everything is updated is because if one of your object can reflect its changes then EVERYTHING will be refreshed. Is i have juste simple objects in my collection or array, the list or dg wont move. If i add a MyCoord1 or a ObjectProxy then i will see all of the lines changing.

           

          Thank you myself