8 Replies Latest reply: Jun 25, 2009 10:18 AM by Joubert Nel RSS

    ArrayCollection vs. Array in HTTPService sending

    Joubert Nel

      Hi,

       

      I want to send the content of an ArrayCollection as one of the parameters in an HTTPService.

      However, Flex concatenates the collection's values into one long string and sends it as the value of a single parameter.

       

      Doing this with arrays work just fine, e.g. in AS3:

           service.send( { name=["john", "lucy", "mark"] });

       

          (also works in MXML using parameter binding)

       

       

      So then I tried first converting the ArrayCollection using .toArray()   -  this is done in MXML, e.g.:

           name="{controller.name.toArray()}"

       

      But, then Flex thinks there is NO value - i.e. data binding to the result of .toArray() doesn't keep track of changes to the underlying ArrayCollection.

       

      Is there any way to databind a single HTTPService parameter to an ArrayCollection like one can do against an Array?

       

      Joubert

        • 1. Re: ArrayCollection vs. Array in HTTPService sending
          rtalton Community Member

          The HHTPService.send() method accepts an object as a parameter. The ArrayCollection is a collection of objects, not just a single object.

          You could split the ArrayCollection into its separate objects, and send each of those individual obejcts in the same send() method as parameters.

           

          The toArray() method will only return the ArrayCollection object, since it is already an object.

          • 2. Re: ArrayCollection vs. Array in HTTPService sending
            Joubert Nel Community Member

            I would think that, conceptually, one should be able to pass an ArrayCollection in the same way that one can pass an Array (which is also a set of objects).

             

            When you pass an Array as the value of an HTTPService parameter, Flex takes it apart and constructs a URL in the form of:

               paramName=arrayVal1&paramName=arrayVal2&paramName=arrayVal3

             

            But not so with an ArrayCollection.

             

            Don't you think?

            • 3. Re: ArrayCollection vs. Array in HTTPService sending
              rtalton Community Member

              The array is a single object. The array collection is a collection of array objects. Hence the different behavior.

              • 4. Re: ArrayCollection vs. Array in HTTPService sending
                Joubert Nel Community Member

                An ArrayCollection is not a collection of array objects.

                 

                It is a wrapper class that exposes a (single) Array object as a Collection.

                • 5. Re: ArrayCollection vs. Array in HTTPService sending
                  rtalton Community Member

                  It will be easier to show you an example of the ArrayCollection. Run this example with a line break at the trace(); statement, and examine the resulting ArrayCollection's items. Once you see this, I think you'll understand why your code isn't working as you expect.

                   

                  The ArrayCollection is not simply a way to store or make a single Array bindable. It is a collection of objects which you can bind to. Its methods allow you to add, delete and search for items within it. You can, in fact, store many arrays within it. And those arrays could be using Flex components as object keys. Even if you send this to a WebService, that service will likely not understand it.

                   

                  <?xml version="1.0" encoding="utf-8"?>
                  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
                       creationComplete="testIt()">
                  <mx:Script>
                      <![CDATA[
                          import mx.controls.ComboBox;
                          import mx.collections.ArrayCollection;
                          import flash.utils.Dictionary;       
                          //Create different types of arrays:
                          public var indexedArray:Array = new Array("john", "lucy", "mark");       
                          public var associativeArray:Array = new Array([
                          {label:"One", data:"7000"},
                          ]);
                          public var dictionaryArray:Dictionary = new Dictionary();
                          //Create objects for a dictionary array:
                          public var objectOne:Object = new Object();
                          public var objectTwo:Object = new Object();
                          public var sprite1:Sprite = new Sprite;
                          public var comboBox1:ComboBox = new ComboBox;
                          //Create an ArrayCollection:
                          private var myAC:ArrayCollection = new ArrayCollection;
                         
                          //This function populates the ArrayCollection:
                          private function testIt():void{
                              //Add values to the objects:
                              objectOne = "This is a Sprite Object";
                              objectTwo = "This is a ComboBox Object";
                              //Add objects to the Dictionary:
                              dictionaryArray[sprite1] = objectOne;
                              dictionaryArray[comboBox1] = objectTwo;
                              //Add all the Arrays to the Collection:
                              myAC.addItem(indexedArray);
                              myAC.addItem(associativeArray);
                              myAC.addItem(dictionaryArray);
                              trace();
                          }      
                      ]]>
                  </mx:Script> 
                  </mx:Application>

                  • 6. Re: ArrayCollection vs. Array in HTTPService sending
                    Joubert Nel Community Member

                    Not sure whether I'm not expressing myself clearly.

                     

                    An Array can hold the same objects that an ArrayCollection does. For example, an Array can be multidimensional.

                     

                    What is nice about the HTTPService is that when it encounters an Array parameter value, it uses a certain algorithm to get a string representation of each of the Array's objects that it then passes as parameters in the URL when the HTTP Service is called.

                     

                    But when it comes to an ArrayCollection, it uses a totally different algorithm - see my first post.

                    And this is what doesn't make sense. It should really use the same algorithm to extract values; there's no reason not to.

                    • 7. Re: ArrayCollection vs. Array in HTTPService sending
                      rtalton Community Member

                      With my example, you can see why HTTPService would not know what is in an ArrayCollection. Nor should we expect it to. The Collection class was designed with the Flex data components in mind. That said, you might consider using bindable value objects in your application. These can be passed to web services in Cold Fusion, PHP, and .NET, allowing you to use data from Flex components, like Forms, directly (which is maybe what you are trying to accomplish? I notice the "name" data in your original post).

                      I pass strongly-typed value objects to .NET web services using the HTTPService.send() method, example: myWebService.send(customerVO), where "customerVO" is a value object bound to a Form in Flex, containing strings, booleans, dates and numbers.

                       

                      .NET can work directly with this value object, retaining the data types with no conversions. So I have zero issues with attempting to store incorrectly-typed data in my SQL tables. Data validation is also easier and more efficient on the .NET side this way. So, you can see why using a VO would have many benefits over an array when working with complex data. And arrays are not bindable, anyway.

                       

                      HTH

                      • 8. Re: ArrayCollection vs. Array in HTTPService sending
                        Joubert Nel Community Member

                        Why could HTTPService not know what is in an ArrayCollection?

                        It can determine what is in an Array.....