Flex issue with single-element vs multi-element

For those of you who have been using Flex since 1.5, do you remember how Flex always had a problem returning single-element arrays as a non-array object, and how we had to apply mx.utils.ArrayUtils to force it to become a one-element array?

It seems like Flex 3.0 still has the same problem. If the XML I am getting from the server looks like this:

<root>
    <node ..../>
    <node ..../>
<root>

that is, a root tag with more than one child node, event.result.root.node will be of type ArrayCollection. But if the XML is like this:

<root>
    <node ..../>
</root>

where there is only one child tag, event.result.root.node becomes of type mx.utils.ObjectProxy. This is causing my application to bomb because I'm automatically storing the results in an ArrayCollection.

Has anyone had this problem before? Any suggestions on how to handle this would be greatly appreciated.

Thanks.
elepilAsked:
Who is Participating?
 
lexxwernCommented:
>> that is, a root tag with more than one child node, event.result.root.node will be
>> of type ArrayCollection. But if the XML is like this:

I believe in this case the event.result.root.node will now (Flex 2 & 3) be of type XMLList even if there are multiple or single node objects in the XML. So I think this should solve your problem if you can convert the XMLList to an ArrayCollection.

Or you can consider using an XMLListCollection which is an implementation of a Collection onto an XMLList.
0
 
elepilAuthor Commented:
You are right, that storing the xml data coming from the server in an XMLListCollection would avoid this problem. While waiting for a response on this issue, I changed my HTTPService's resultFormat to 'e4x', received the data in XML format, then *manually* parsed it to construct an ArrayCollection. I rated 'partial' to your solution because you didn't show how to convert an XMLListCollection object to an ArrayCollection. Because I did it differently from the way you're recommending (and my way was tedious, too), it would've been nice to see if there was an easy and quick way to convert an XMLListCollection to an ArrayCollection.

Nevertheless, I gave you full credit anyway.
0
 
lexxwernCommented:
>> it would've been nice to see if there was an easy and quick way to convert
>> an XMLListCollection to an ArrayCollection

Yeah probably you could just explicitly type cast it.. try something like this:

   var newArrayCollection:ArrayCollection = theXmlListCollection as ArrayCollection;
   // or
   var newArrayCollection:ArrayCollection = ArrayCollection(theXmlListCollection);

0
 
elepilAuthor Commented:
I didn't expect XMLListCollection and ArrayCollection to be interchangeably cast-able; I am glad it is that easy. Thanks for following up.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.