6 Replies Latest reply on Jun 18, 2008 8:26 PM by whiterrabbit

    ArrayCollections with only one item

    whiterrabbit Level 1
      I have a job tracking board I am building. If the salesrep has a few open jobs, no problem. If the salesrep has only one open job, Flex throws a fit. What is the best way to handle this. Why do array collections need multiple items to work properly?

      I am using the php code generated with the Flex Wizzard to output to "The Std Response Structure" as attached. On the Flex Side I am doing this - joblistings = event.result.response.data.row; - in my event handler.

      Thanks for your help

        • 1. Re: ArrayCollections with only one item
          smgilson Level 1
          I'm not sure if I quite understand, but here is some info that might help. If you are unsure whether the result of a service call contains an Array or an individual object, use the toArray() method of the mx.utils.ArrayUtil class to convert it to an Array. If you pass the toArray() method an individual object, it returns an Array with that object as the only Array element. If you pass an Array to the method, it returns the same Array.

          Stephen
          • 2. Re: ArrayCollections with only one item
            whiterrabbit Level 1
            I think you are understanding correctly ...
            You gave me some ideas, I will try out tomorrow,

            Thanks

            • 3. ArrayCollections with only one item
              whiterrabbit Level 1
              I have been testing but can't figure this out. I have attached the xml output for a recordset with only one item, looks okay to me.

              Now from Flex side (inside Event Handler) I check for number of rows
              var tempcount:Number = event.result.response.data.row.length;
              trace(tempcount);

              If there are more than one item in the result set - tempcount traces out correctly
              if there is only one item in tempcount - tempcount traces out not a number NAN

              Any ideas.
              ___________Edited___________
              I just want to add that when you trace out trace(event.result.response.data.row); this is what you get:
              on a result set containing say 3 rows you get [object Object],[object Object],[object Object]
              trace out the length and it = 3 as expected.

              on a result set containing only one row you get the expected [object Object]
              trace out the length and it outputs NaN.
              • 5. Re: ArrayCollections with only one item
                VarioPegged Level 2
                Can you specify a different resultFormat and XMLListCollection or are you forced to use the default of "object" and ArrayCollection? Working with E4X to handle XML along with XMLList (or XMLListCollection) might be more predictable for what you're trying to do. So if you can specify a resultFormat="e4x" in your service within Flex, then you should get your result:

                var tempcount:int = new XMLListCollection(event.result.data.row).length;

                Notice how in E4X the root node ("response" here) is omitted from the argument you pass to the constructor.

                TS
                • 6. ArrayCollections with only one item
                  whiterrabbit Level 1
                  Thanks,

                  I just used the function provided at the link above and it works fine. Sucks that I lost two days trying to figure this out though. This is the type of thing that should be in the documentation, I read all of it over and over ,,, thats why the grrrr. :)

                  Here is the function in case anyone else wonders why result sets of one aren't showing up.

                  private function resultHandler(event :ResultEvent ) :void {

                  if( event.result.dataroot.city == null ) {
                  mx.controls.Alert.show("The response data are empty !");
                  }
                  else if ( event.result.dataroot.city is ObjectProxy ) {
                  // the response date has only one item
                  myAC = new ArrayCollection( [event.result.dataroot.city ] );
                  }
                  else {
                  myAC = event.result.dataroot.city as ArrayCollection;

                  }
                  }

                  use- event.result.response.data.row for your data model if your using the flex generated php and Pear searializer class.

                  EDITED
                  BTW - if it is an empty record set, I am still showing errors checking if repeating object is null "row"
                  If you back up one ie if(event.result.response.data == null), that works on empty records sets.