8 Replies Latest reply on Nov 24, 2011 4:47 AM by ravid.paldi

    help with retreiving data with remoteObjects

    ravid.paldi

      Hi,

       

      I'm very new to Flex and i'm trying to retreive data from a DB. I have created a service for the data, connected it and tested it to see that its actually retreiving the data. My problem is that for somereason I don't manage to use the service properly.

       

      my code is:

      "

      <fx:Script>

      <![CDATA[

      [Bindable]

      protected var groups:ArrayList;

       

      protected function list_creationCompleteHandler(event:FlexEvent):void

      {

      getGroupsResponse.token = getGroups.getAllGroups();

      groups = getGroupsResponse.lastResult;               

      }

       

      ]]>

      </fx:Script>

      <fx:Declarations>

      <s:CallResponder id="getGroupsResponse"/>

      <groupservice:GroupService id="getGroups"

      fault="Alert.show(event.fault.faultDetail)"/>

      </fx:Declarations>

      "

      when I'm debugging the "groups" var it shows that it still null even after the assignment, and if I try to use the groups var as a data provider it indeed show enpty list. BUT if I try direct binding with the lastResult as a data provider everything works.

       

      Can someone help me and tell me what i'm doing wrong?

       

      Thanks in advance

      Ravid

        • 1. Re: help with retreiving data with remoteObjects
          Don Kerr Level 3

          Not sure what format your service is returning, but try

           

          groups = getGroupsResponse.lastResult as ArrayList;

           

           

          Don

          • 2. Re: help with retreiving data with remoteObjects
            ravid.paldi Level 1

            Thanks Don,

             

            Its returning an array of groups. each group has 2 properties - id and name. (group is a value object)

             

            I tried adding "As ArrayList" but i get this error: "

            DescriptionResourcePathLocationType
            1067: Implicit coercion of a value of type mx.collections:ArrayList to an unrelated type mx.rpc:AsyncToken.autoTradeFlex.mxml/autoTradeFlex/srcline 22Flex Problem
            • 3. Re: help with retreiving data with remoteObjects
              ravid.paldi Level 1

              for some reason after i do the assignment: "groups = getGroupsResponse.lastResult"

              the value in groups become "null" (even if I initiate a new array on the groups declartion)

               

              any clue what it could be?

              • 4. Re: help with retreiving data with remoteObjects
                Don Kerr Level 3

                When you bind lastResult it to the List's dataProvider directly, check the variables tab in debug.  What type is yourlist.dataProvider?

                • 5. Re: help with retreiving data with remoteObjects
                  ravid.paldi Level 1

                  dataProvider

                  mx.collections.AsyncListView (@6faf3d1)

                  [inherited]

                  createFailedItemFunction<getter>

                  _createFailedItemFunctionFunction (@6faf421)

                  createPendingItemFunction<getter>

                  _createPendingItemFunctionFunction (@6faf3f9)

                  length0

                  listnull

                  _listnull

                   

                  The genrated code (when I drag a list and than drag the service to it) is:

                  <s:List id="list" x="166" y="183" creationComplete="list_creationCompleteHandler(event)" labelField="id">

                       <s:AsyncListView list="{getAllGroupsResult.lastResult}"/>

                  </s:List>

                   

                  if i try to do:

                  <s:List id="list" x="166" y="183"

                             creationComplete="list_creationCompleteHandler(event)"

                             labelField="id" 

                            dataProvider="{getAllGroupsResult.lastResult}">

                  </s:List>

                  I get this error:

                  DescriptionResourcePathLocationType
                  Initializer for 'dataProvider': values of type mx.collections.IList cannot be represented in text.autoTradeFlex.mxml/autoTradeFlex/srcline 42Flex Problem
                  • 6. Re: help with retreiving data with remoteObjects
                    Don Kerr Level 3

                    I've never used the AsyncListView collection... but it is not an Array/ArrayList that is coming back.   It looks like you'd need to convert it to an ArrayList if you want it to work by assigning it to your groups ArrayList.

                     

                    My guess is that It probably requires direct binding since it is async /paging.

                    1 person found this helpful
                    • 7. Re: help with retreiving data with remoteObjects
                      Don Kerr Level 3

                      BTW, if you don't require paging, there are other options.  Here is some sample code from tourdeflex below ...

                       

                      This similar to how I do it all the time vs. using paging/async .

                       

                      In the remoteObject's result you set it to an ArrayCollection. 

                       

                      private function getProducts_resultHandler(event:ResultEvent):void

                         {

                         products = event.result as ArrayCollection;

                         }

                       

                      In your case it would be an ArrayList.

                       

                       

                      <?xml version="1.0" encoding="utf-8"?>

                      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009
                                     xmlns:s="
                      library://ns.adobe.com/flex/spark"
                                     xmlns:mx="
                      library://ns.adobe.com/flex/mx"
                                     skinClass="
                      TDFGradientBackgroundSkin" viewSourceURL="srcview/index.html">

                         

                          <fx:Script>

                              <![CDATA[

                                  import mx.collections.ArrayCollection;

                                  import mx.controls.Alert;

                                  import mx.rpc.events.FaultEvent;

                                  import mx.rpc.events.ResultEvent;

                                 

                                  []private var products:ArrayCollection;

                                 

                                  private function getProducts_resultHandler(event:ResultEvent):void

                                  {

                                      products = event.result as ArrayCollection;

                                  }

                                 

                                  private function faultHandler(event:FaultEvent):void

                                  {

                                      Alert.show(event.fault.faultDetail, "Error");

                                  }

                                 

                              ]]>

                          </fx:Script>

                         

                          <fx:Declarations>

                              <s:ChannelSet id="channelSet">

                                  <s:AMFChannel id="amfChannel" url="http://tourdeflex.adobe.com:8080/lcds-samples/messagebroker/amf"/>

                              </s:ChannelSet>

                             

                              <!-- RemoteObject calls are asynchronous. The result event is triggered when the service invocation
                              returns. The fault event is triggered if an error occurs at the server-side, or if the network
                              becomes unavailable. You can define result and fault handlers at the RemoteObject level or at the
                              method level.
                      -->

                              <s:RemoteObject id="srv"
                                              destination="
                      product"
                                              channelSet="
                      {channelSet}"
                                              fault="
                      faultHandler(event)">

                                 

                                  <s:method name="getProducts" result="getProducts_resultHandler(event)"/>

                                 

                                  <!-- <mx:method name="updateProduct" result="updateProduct_resultHandler(event)"/> -->

                                 

                              </s:RemoteObject>

                             

                          </fx:Declarations>

                         

                          <s:layout>

                              <s:HorizontalLayout horizontalAlign="center" />

                          </s:layout>      

                         

                          <s:Panel title="RemoteObject Events"
                                   color="
                      0x000000"
                                   borderAlpha="
                      0.15"
                                   width="
                      600">

                             

                              <s:layout>

                                  <s:VerticalLayout paddingLeft="10"
                                                    paddingRight="
                      10"
                                                    paddingTop="
                      10"
                                                    paddingBottom="
                      10"/>

                              </s:layout>

                             

                              <mx:DataGrid dataProvider="{products}" width="100%" height="100%">

                                  <mx:columns>

                                      <mx:DataGridColumn dataField="productId" headerText="Product Id"/>

                                      <mx:DataGridColumn dataField="name" headerText="Name"/>

                                      <mx:DataGridColumn dataField="price" headerText="Price" textAlign="right"/>

                                      <mx:DataGridColumn dataField="qtyInStock" headerText="Qty" textAlign="right"/>

                                  </mx:columns>

                              </mx:DataGrid>

                             

                              <s:Button label="Get Data" click="srv.getProducts()"/>  

                             

                          </s:Panel>

                         

                      </s:Application>

                      • 8. Re: help with retreiving data with remoteObjects
                        ravid.paldi Level 1

                        Thanks Don,

                        Do you know how I can make my service return an array of my VOs?

                        I've created the services using the automatic service generator that comes with flash builder 4.5 for PHP.

                        I'll try to look it up in the web.

                        Thanks again.