11 Replies Latest reply on May 2, 2007 8:14 PM by Stew Padaso

    Convert object to arraycollections

    Stew Padaso
      I've read in the forums that when you are returning one element from XML, Flex does not return the xml as an array but rather an object. I have tried to put returnFormat="array" into the HTTPService line as well as convert the object to an array in 'getPhotoList' function using this: event.result.gallery.photo = ArrayUtil.toArray(event.result.gallery.photo);

      I have been unsuccessful using both methods, any suggestions?

      <mx:HTTPService id="photoListRequest" url="list.php?type=customerImage" result="getPhotoList(event)" method="POST" >
      <mx:request xmlns="">
      <user_id>{user_id}</user_id>
      </mx:request>
      </mx:HTTPService>
      <mx:Script>
      <![CDATA[
      [Bindable] private var fileXMLList:ArrayCollection;
      private function getPhotoList(event:ResultEvent):void {
      photoXMLList = event.result.gallery.photo;
      }
      ]]>
      </mx:Script>
        • 1. Re: Convert object to arraycollections
          dimival Level 1
          What happens if you do this:

          array1 = event.result.gallery.photo.toArray();

          Dont worry if the IDE doesn't recognize it as a function, just compile it and try it out
          • 2. Re: Convert object to arraycollections
            Stew Padaso Level 1
            I get the following runtime error:

            TypeError: Error #1010: A term is undefined and has no properties.
            at mx.utils::ObjectProxy/ http://www.adobe.com/2006/actionscript/flash/proxy::callProperty()
            at SBC/::getPhotoList()
            at SBC/__photoListRequest_result()
            at flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEventFunction()
            at flash.events::EventDispatcher/dispatchEvent()
            at mx.rpc.http.mxml::HTTPService/ http://www.adobe.com/2006/flex/mx/internal::dispatchRpcEvent()
            at mx.rpc::AbstractInvoker/ http://www.adobe.com/2006/flex/mx/internal::resultHandler()
            at mx.rpc::Responder/result()
            at mx.rpc::AsyncRequest/acknowledge()
            at ::DirectHTTPMessageResponder/completeHandler()
            at flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEventFunction()
            at flash.events::EventDispatcher/dispatchEvent()
            at flash.net::URLLoader/flash.net:URLLoader::onComplete()
            • 3. Convert object to arraycollections
              EliteScientist Level 1
              try:
              ArrayUtil.toArray(object);

              located in:

              mx.utils.ArrayUtil

              for array collection:

              var collection:ArrayCollection = new ArrayCollection(ArrayUtil.toArray(object));
              • 4. Re: Convert object to arraycollections
                dimival Level 1
                Which is the name of the property in your result object that is an Array?
                photo?
                • 5. Re: Convert object to arraycollections
                  Stew Padaso Level 1
                  quote:

                  Originally posted by: BaTMaNX
                  try:
                  ArrayUtil.toArray(object);

                  located in:

                  mx.utils.ArrayUtil

                  for array collection:

                  var collection:ArrayCollection = new ArrayCollection(ArrayUtil.toArray(object));


                  Sweet, I just needed to take it one step further and convert the array to an arraycollection. Thanks. So how do I handle both one and multiple element returns. I have tried this (below), but I am getting another runtime error.

                  private function getPhotoList(event:ResultEvent):void {
                  if (event.result.gallery.photo is Object)
                  photoXMLList = new ArrayCollection(ArrayUtil.toArray(event.result.gallery.photo));
                  else
                  photoXMLList = event.result.gallery.photo;
                  }
                  • 6. Re: Convert object to arraycollections
                    Stew Padaso Level 1
                    quote:

                    Originally posted by: dimival
                    Which is the name of the property in your result object that is an Array?
                    photo?


                    yes it would be photo, here is the xml return with one element.

                    <gallery>
                    <photo>
                    <unique_id>9d68f1bbf165d2e5ae17a8d230a934f6</unique_id>
                    <width>600</width>
                    <height>450</height>
                    </photo>
                    </gallery>
                    • 7. Re: Convert object to arraycollections
                      Stew Padaso Level 1
                      ok, if I return just one element, the following works:
                      photoXMLList = new ArrayCollection(ArrayUtil.toArray(event.result.gallery.photo));

                      but if I return more than one element, the following works:
                      photoXMLList = event.result.gallery.photo;

                      I've tried multiple different ways (below) to add a condition, none which work...any suggestions?

                      private function getPhotoList(event:ResultEvent):void {
                      if (event.result.gallery.photo is Object)
                      photoXMLList = new ArrayCollection(ArrayUtil.toArray(event.result.gallery.photo));
                      else
                      photoXMLList = event.result.gallery.photo;
                      }



                      • 8. Re: Convert object to arraycollections
                        Nate Weiss
                        I'm just looking at this quickly, but how about:

                        if ( event.result.gallery.photo is Array ) {
                        photoXMLList = event.result.gallery.photo;
                        } else {
                        photoXMLList = new ArrayCollection(ArrayUtil.toArray(event.result.gallery.photo)); }

                        Also, regardless of the condition, if you want an ArrayCollection, maybe you need to do:

                        photoXMLList = new ArrayCollection(event.result.gallery.photo);

                        instead of

                        photoXMLList = event.result.gallery.photo;
                        • 9. Re: Convert object to arraycollections
                          Stew Padaso Level 1
                          quote:

                          Originally posted by: Stew Padaso
                          ok, if I return just one element, the following works:
                          photoXMLList = new ArrayCollection(ArrayUtil.toArray(event.result.gallery.photo));

                          but if I return more than one element, the following works:
                          photoXMLList = event.result.gallery.photo;

                          I've tried multiple different ways (below) to add a condition, none which work...any suggestions?

                          private function getPhotoList(event:ResultEvent):void {
                          if (event.result.gallery.photo is Object)
                          photoXMLList = new ArrayCollection(ArrayUtil.toArray(event.result.gallery.photo));
                          else
                          photoXMLList = event.result.gallery.photo;
                          }






                          According to the debugging tools, both results (one element return or multiple element return) are of type ObjectProxy. Is there another way to determine the number of results? I tried 'event.result.gallery.length' but was unsuccessful. If not, is there a way to differentiate between the two results. I'm so close to finishing this project, just need to get this figured out.
                          • 10. Re: Convert object to arraycollections
                            brado77
                            Was this problem ever solved? I have the same issue -- my code works fine if the xml returned has multiple items, but with one item, the app blows up. Is there a single way to create an ArrayCollection that works regardless of the number of xml items returned, and if not, what is the proper way to handle this?

                            Thanks,

                            Brad
                            • 11. Re: Convert object to arraycollections
                              Stew Padaso Level 1
                              Brad,

                              Here is the solution I found:

                              private function getPhotoList(event:ResultEvent):void {
                              try {
                              photoXMLList = event.result.gallery.photo;
                              }
                              catch(e:Error) {
                              photoXMLList = new ArrayCollection();
                              photoXMLList.addItem(event.result.gallery.photo);
                              }
                              }