Skip navigation
Joubert Nel
Currently Being Moderated

ArrayCollection vs. Array in HTTPService sending

Jun 23, 2009 5:11 AM

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

 
Replies
  • rtalton
    1,133 posts
    Oct 28, 2008
    Currently Being Moderated
    Jun 23, 2009 7:34 AM   in reply to Joubert Nel

    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.

     
    |
    Mark as:
  • rtalton
    1,133 posts
    Oct 28, 2008
    Currently Being Moderated
    Jun 23, 2009 8:20 AM   in reply to Joubert Nel

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

     
    |
    Mark as:
  • rtalton
    1,133 posts
    Oct 28, 2008
    Currently Being Moderated
    Jun 23, 2009 9:16 AM   in reply to Joubert Nel

    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>

     
    |
    Mark as:
  • rtalton
    1,133 posts
    Oct 28, 2008
    Currently Being Moderated
    Jun 25, 2009 10:03 AM   in reply to Joubert Nel

    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

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points