1 Reply Latest reply on Jan 4, 2008 12:06 PM by m_hartnett

    HTTPService XML to ArrayCollection

    oakmad
      I'm pretty sure this is due to my lack of experience but if anyone could help me out I'd be real grateful. I have the following code working when my search returns multiple results, but it breaks when here is only one. To fix I tried the recommended toArray function but now the arraycollection is full of objects and get error when accessing it. Can someone help me out on how I need to retrieve records from the arraycollection of objects?

      Search returns this XML with product repeated for each result:
      <products>
      <product id="12345678">
      <product_title>Widget A</product_title>
      <description>This is my widget description</description>
      <image title="image title">
      <thumbnail> http://xx.yy.zz</thumbnail>
      <main> http://xx.yy.zz</main>
      </image>
      </product>
      <product id="234567">
      ...
      </product>
      </products>

      The following code works when more than one result returned:
      private function searchHandler(event:ResultEvent):void {
      styles = new ArrayCollection();
      try {
      styles = event.result.products.product;
      selectedProduct = 0;
      }
      catch (e:Error) {
      Alert.show("Search returned no results.\n Try a simpler search term.","Oops",mx.controls.Alert.OK);
      }
      }

      Output:
      <mx:Repeater id="styleRepeater" dataProvider="{styles}">
      <mx:VBox width="100%" height="100%" horizontalAlign="center" textAlign="center" verticalScrollPolicy="off" horizontalScrollPolicy="off">
      <mx:Image id="pic"
      source="{styleRepeater.currentItem.image.thumbnail}"
      scaleContent="true"
      autoLoad="true"
      width="100"
      height="100"/>
      <mx:LinkButton
      id="prodName"
      label="{styleRepeater.currentItem.product_title}"
      click="editProduct(event.target.getRepeaterItem())"
      />
      </mx:VBox>
      </mx:Repeater>

      To attempt to make it work I changed the search handler to the following and left the output same.
      private function searchHandler(event:ResultEvent):void {
      try {
      styles = new ArrayCollection(ArrayUtil.toArray(event.result.products.product));
      trace(styles);
      selectedProduct = 0;
      }
      catch (e:Error) {
      Alert.show("Search returned no results.\n Try a simpler search term.","Oops",mx.controls.Alert.OK);
      }
      }

      The error I am getting through the debugger is:
      warning: unable to bind to property 'product_title' on class 'mx.collections::ArrayCollection'

      when I look at the trace of the styles collection its showing a collection of objects, so I assume I need to modify my output somehow to reflect this but am stumped.

      Any ideas?

      Thanks

      Adam