5 Replies Latest reply on Mar 21, 2007 5:06 PM by dimival

    Unexpected Data Binding

    cialeen
      Hi, all

      I am always confused about data binding. It brings much convinience, but I always get some unexpected data binding.

      I specified an array as the dataprovider for the chart, and the array will be updated which is based on the data range user type in later on.

      But I stil want to get the reference of the original array, becase next time user change the data range I want it on the base of the original array, not the one already changed.

      // testArray is bindable.
      lineSeries.dataProvider = this.testArray;
      ...
      private function rangeChange():void
      {
      ...
      // Here, originalArray is the same as testArray except it is not specified with Bindable.
      lineSeries.dataProvider = this.originalArray;

      var _dataArray:ArrayCollection = _lineSeries.dataProvider as ArrayCollection;
      for ( var i:Number = 0; i<_dataArray.length; i++)
      {
      var _data:ObjectProxy = _dataArray as ObjectProxy;
      if (scaleFrom > _data.value)
      {
      _data.value = 0;
      }
      else
      {
      if ( scaleTo < _data.value)
      {
      _data.value = 100;
      }
      else
      {
      _data.value = (_data.value - scaleFrom) * 100/ (scaleTo - scaleFrom);
      }
      ..
      }

      If I code like this, the data in the dataProvider, testArray, originalArray all will be updated which is not I want. I wanna keep the originalArray unchanged!

      Please help me, thank you very much.
        • 1. Unexpected Data Binding
          dimival Level 1
          Flex uses references, so no matter what you do as long as you do something like this:
          array1 = array2

          you will always have that issue, because flex uses references.

          If you want to have an original array that can't be modified like that, you will need to create a new one and add item per item of the array:

          var newArray:Array;
          for(var i = 0; i<array.length; i++) {
          newArray.push(array );
          }

          This way, your original array wont be modified when you make changes on the other one.

          ActionScript 3 uses references instead of values as explained on page 55 of the "Programming ActionScript 3.0" document, which you can find in the livedocs.

          Hope this helps
          • 2. Re: Unexpected Data Binding
            cialeen Level 1
            Thank you for your replay. But it still changed all the time.

            var newArray:Array = new Array();

            for(var j:Number = 0; j< _lineSeries.OriginalDataProvider.length; j++)
            {
            newArray.push(_lineSeries.OriginalDataProvider[j]);
            }

            _lineSeries.dataProvider = newArray;

            Any more ideas?

            Thank you.
            • 3. Re: Unexpected Data Binding
              dimival Level 1
              Well i am doing this and it works, maybe i missed the getItem method in the previous post:

              var arrayTemp : Array = new Array();
              for (i = 0; i < sistemasList.length; i++) {
              arrayTemp.push(sistemasList.getItemAt(i));
              }
              sistemasDisponiblesList :ArrayCollection= new ArrayCollection(arrayTemp);
              dg.dataProvider = sistemasDisponiblesList;

              I am using this logic in different places of my application and works, try it out.
              • 4. Re: Unexpected Data Binding
                cialeen Level 1
                Thank you for your reply again.

                I tried what you said but it was still not working.
                However, the problem is resovled. What I did is create a new objectproxy each time when looping through the array.

                var newArray:Array = new Array();
                for(var i:Number = 0; i< temp.length; i++)
                {
                var myObj:mx.utils.ObjectProxy = new ObjectProxy();
                myObj.value = temp .value;
                myObj.date = temp
                .date;
                newArray.push(myObj);
                }

                _lineSeries.dataProvider = newArray;

                Anyway, I greatly appreciate your help.
                • 5. Re: Unexpected Data Binding
                  dimival Level 1
                  You made it work, cool :)

                  Still it should have worked, cause that's why i am using and it works fine, weird