9 Replies Latest reply on Jan 10, 2010 7:49 PM by flexy8

    Knowing when the callResponder's lastResult arrives?

    flexy8 Level 1

      is there a way to know when exactly the data arrived at the lastResult of the callResponder so that i would be able to execute another service call when the data arrives?

        • 1. Re: Knowing when the callResponder's lastResult arrives?
          Sameer Bhatt Adobe Employee

          Hook on to the "result" event of the CallResponder.

           

          -Sameer

          1 person found this helpful
          • 2. Re: Knowing when the callResponder's lastResult arrives?
            David_F57 Level 5

            Hi,

             

            you can add a result event handler to your call

             

            <fx:Declarations>

            <s:CallResponder id="myresponse" result="myresponse_resultHandler(event)"

            </fx:Declarations>

             

            protected function myresponse_resultHandler(event:ResultEvent):void

            {

            // TODO Auto-generated method stub

            }

             

             

            David

            1 person found this helpful
            • 3. Re: Knowing when the callResponder's lastResult arrives?
              flexy8 Level 1

              Thanks for the idea. Now, how would I append or concatenate the results of each service call so that it will serve as my dataprovider on my List?

               

              here,s my code:

              <?xml version="1.0" encoding="utf-8"?>
              <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                   xmlns:s="library://ns.adobe.com/flex/spark" 
                   xmlns:mx="library://ns.adobe.com/flex/halo" xmlns:barangayservice="services.barangayservice.*">
                   <fx:Script>
                        <![CDATA[
                             import flash.events.MouseEvent;
                             
                             import mx.collections.ArrayCollection;
                             import mx.controls.Alert;
                             import mx.rpc.events.ResultEvent;
              
                             private var line:RegExp = /([a-zA-Z\-,'ñ0-9 ]+)/g;
                             
                             [Bindable]
                             private var resArr:ArrayCollection;
                             
                             protected function btnSearch_clickHandler(event:MouseEvent):void
                             {
                                  var lineObj:Object = line.exec(taParse.text);
                                  var lineArr:Array = [];
                                  
                                  while (lineObj != null)
                                  {
                                       lineArr.push({vars:lineObj[1]});
                                       lineObj = line.exec(taParse.text);
                                  }
                                  
                                  for(var l:uint = 0; l < lineArr.length; l++)
                                  {
                                       getBarangaysByNameResult.token = barangayService.getBarangaysByName(lineArr[l].vars)
                                  }
              
                             }
              
                             protected function barangayService_resultHandler(event:ResultEvent):void
                             {
                                  resArr.addItem(event.result);
                             }
                        ]]>
                   </fx:Script>
                   <fx:Declarations>
                        <s:CallResponder id="getBarangaysByNameResult"/>
                        <barangayservice:BarangayService id="barangayService" result="barangayService_resultHandler(event)" fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)" showBusyCursor="true"/>
                        <!-- Place non-visual elements (e.g., services, value objects) here -->
                   </fx:Declarations>
                   <s:Button x="146" y="169" label="Button" id="btnSearch" click="btnSearch_clickHandler(event)"/>
                   <s:List x="10" y="203" width="206" id="listSearch" dataProvider="{resArr}" labelField="barangay_name" height="129"></s:List>
                   <s:TextArea x="10" y="10" id="taParse" width="206"/>
                   <s:TextArea x="224" y="10" id="ta"/>
              </s:WindowedApplication>
              
               
              

              • 4. Re: Knowing when the callResponder's lastResult arrives?
                David_F57 Level 5

                Hi,

                 

                The first thing you are going to need is segregation of logic, you can't just loop through the search list as you are doing, you need to have a process

                 

                you need 3 vars declared at the head of your file

                 

                mySearchList : array

                myDataprovider: arraycollection

                MyLoopCount: integer=0

                 

                1. create search list function, the list is created then if it has items it fires a dataretrival function say getMyItem(myLoopCount),

                 

                 

                2. the getMyItem uses the searcharray value in the responder call;

                 

                3. the result function iterates through the result then appends each row to your dataprovider array.

                    inc the loopCounter if the loopcounter is less than the length of the search array call getmyItem again

                 

                 

                David

                • 5. Re: Knowing when the callResponder's lastResult arrives?
                  flexy8 Level 1

                  1. create search list function, the list is created then if it has items it fires a dataretrival function say getMyItem(myLoopCount),

                  Hi David,

                   

                       Thanks so much, I'm getting the idea now that I shouldn't just loop through the function that returns a list of items. But, I don't get this part of your reply on how to do it. How do I know if it has items and how many so that I can fire the getMyItem(myLoopCount) function? Can I just do like

                   

                  myServiceCall.addEventListener("result",
                       function (event:FlexEvent):void
                       {
                            if (event.result) {
                                 // how do i count the results?
                            }
                       }
                  );

                   

                  Anyway, here's the code snippet Iv'e changed from my original code...

                   

                  [Bindable]
                  private var myDp:ArrayCollection;
                  
                  protected function btnSearch_clickHandler(event:MouseEvent):void
                  {
                       var lineObj:Object = line.exec(taParse.text);
                       var lineArr:Array = [];
                       
                       while (lineObj != null)
                       {
                            lineArr.push({vars:lineObj[1]});
                            lineObj = line.exec(taParse.text);
                       }
                       
                       for (var l:uint = 0; l < lineArr.length; l++)
                       {
                            searchArr.push(barangayService.getBarangaysByName(lineArr[l].vars));
                       }
                       
                       for (var resCnt:uint = 0; resCnt < searchArr.length; resCnt++)
                       {
                            myDp.addItem(searchArr[resCnt]);
                       }
                       
                       listSearch.dataProvider.addItem(myDp);
                  }  
                  

                   

                  ...but I'm getting an error when my trigger button is clicked then fires the search function. It says, "TypeError: Error #1009: Cannot access a property or method of a null object reference."  on this line: searchArr.push(barangayService.getBarangaysByName(lineArr[l].vars));

                   

                  Please, I'm really stuck on getting the results and then concatenate it from the other results.

                  • 6. Re: Knowing when the callResponder's lastResult arrives?
                    David_F57 Level 5

                    Hi,

                     

                    this is slightly more structured, the idea is to allow one set of results to return before we request another set.

                     

                    David

                     

                    <?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" minWidth="955" minHeight="600">

                    <fx:Script>

                    <![CDATA[

                    import mx.collections.ArrayCollection;

                    import mx.rpc.events.FaultEvent;

                    import mx.rpc.events.ResultEvent;

                     

                    private var searchArr: Array = new Array();

                    [Bindable] private var myData: ArrayCollection= new ArrayCollection();

                    private var searchLoop: int = 0;

                     

                    protected function myResponder_resultHandler(event:ResultEvent):void

                    {

                    //myResponder.lastResult  ===> to myData

                     

                    // now get ready for the next possible search call this will only trigger will there are search records to iterate through

                    searchLoop +=1;

                    if (searchLoop < searchArr.length)

                    {

                    getMyData(searchLoop);

                    }

                     

                    }

                     

                    private function searchList(): void

                    {

                    //assume its searching 3 items

                    searchLoop = 0;

                    searchArr.push("peter");

                    searchArr.push("paul");

                    searchArr.push("mary");

                    if (searchArr.length > 0) getMyData(0);

                    }

                     

                     

                    private function getMyData(value:int): void

                    {

                    // call the database function and pass the searchArr(value) to it;

                    //so the passed string for searchArr[1] would be 'paul'

                    //I have a search function on the back-end called getmysearchstuff

                    myResponder.token = dataService.getmysearchstuff(searchArr[value]);

                    }

                     

                    ]]>

                    </fx:Script>

                    <fx:Declarations>

                    <s:CallResponder id="myResponder" result="myResponder_resultHandler(event)"/>

                    <dataservice:dataService id="dataService" showBusyCursor="true"/>

                    </fx:Declarations>

                    </s:Application>

                    • 7. Re: Knowing when the callResponder's lastResult arrives?
                      flexy8 Level 1

                      Hi David,

                           Iv'e been busy with my other school projects that's why I kindah late in reading your reply and I really2x appreciate your help. It worked for me!  Thanks so much. But I have just a little question regarding of the returned item/s from the service call.

                       

                           The code works so well if it returns a String. But since it is a search function from a database, it may return rows and in that case, it will be an array of rows. My only concern is, when I configured the return type of my service call to it's array data type (Barangay []), the data that is displayed on the List is only one "[object Datalist]" and if I change it to another return type, an Object data type, it returns the correct number of results but the output that will be displayed on the List is "[object Object]" only in one row of the List ([object Object], [object Object], [object Object]....).

                       

                           Question:

                                Is there a way to access the targeted or the intended data? I tried "myArrayCollectionVar.barangay_name" but it will produce an error on my List.

                       

                      OR

                                To avoid the returned multiple rows, this maybe an off-topic here in this forum but if you mind if you could help on how to search and exact string from a database so that it will return only one result? I tried " SELECT * FROM barangays WHERE `barangay_name` = '$query' " but it dint work for me.

                       

                      varchar(50)latin1_swedish_ciYesNULL

                       

                       

                      Anyway, here's the working code.

                       

                      <?xml version="1.0" encoding="utf-8"?>
                      <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                           xmlns:s="library://ns.adobe.com/flex/spark" 
                           xmlns:mx="library://ns.adobe.com/flex/halo" xmlns:barangayservice="services.barangayservice.*" width="227" height="345">
                           <fx:Script>
                                <![CDATA[
                                     import mx.rpc.events.ResultEvent;
                                     import flash.events.MouseEvent;
                                     
                                     import mx.collections.ArrayCollection;
                                     import mx.controls.Alert;
                                     
                                     import valueObjects.Barangay;
                      
                                     private var line:RegExp = /([a-zA-Z\-,'ñ0-9 ]+)/g;
                                     
                                     
                                     [Bindable] 
                                     private var myData:ArrayCollection = new ArrayCollection();
                                     private var searchArr:Array;
                                     private var searchLoop:int = 0;
                                     
                                     private function btnSearch_clickHandler(event:MouseEvent):void
                                     {
                                          var lineObj:Object = line.exec(taParse.text);
                                          searchArr = []
                                          searchLoop = 0;
                                          
                                          while (lineObj != null)
                                          {
                                               searchArr.push({name:lineObj[1]});
                                               lineObj = line.exec(taParse.text);
                                          }
                                          
                                          if (searchArr.length > 0) getMyData(0);
                                     }
                      
                                     private function getBarangaysByNameResult_resultHandler(event:ResultEvent):void
                                     {
                                          myData.addItem(getBarangaysByNameResult.lastResult);
                                          searchLoop++;
                                          if (searchLoop < searchArr.length) {
                                               getMyData(searchLoop);
                                          }
                                     }
                                     
                                     private function getMyData(value:int):void
                                     {
                                          getBarangaysByNameResult.token = barangayService.getBarangaysByName(searchArr[value].name);
                                     }
                      
                                ]]>
                           </fx:Script>
                           <fx:Declarations>
                                <s:CallResponder id="getBarangaysByNameResult" result="getBarangaysByNameResult_resultHandler(event)"/>
                                <barangayservice:BarangayService id="barangayService" fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)" showBusyCursor="true"/>
                                <!-- Place non-visual elements (e.g., services, value objects) here -->
                           </fx:Declarations>
                           <s:Button x="146" y="169" label="Button" id="btnSearch" click="btnSearch_clickHandler(event)"/>
                           <s:List x="10" y="203" width="206" id="listSearch" dataProvider="{myData}" labelField="barangay_name" height="129"></s:List>
                           <s:TextArea x="10" y="10" id="taParse" width="206"/>
                      </s:WindowedApplication>
                      

                       

                      Thanks..

                      • 8. Re: Knowing when the callResponder's lastResult arrives?
                        David_F57 Level 5

                        Hi,

                         

                        I will have a look at this in the morning (its 3am here and the temperature has finally dropped below 30c so might be cool enough to sleep).

                         

                         

                        David

                        • 9. Re: Knowing when the callResponder's lastResult arrives?
                          flexy8 Level 1

                          Ok David, thanks in advance.

                          Anyway, here's what I have played around and I'm wondering why the List isn't displaying the intended output.

                           

                          ta.text+=myData.length + " - " + getBarangaysByNameResult.lastResult.length + " - " + myData[0][1].barangay_name;

                          This is what I have added and I was surprised that myData is getting the correct values returned from a database query but the List isn't doing well with the data. This text area will produce "1 - 7 - Cebu". The 2nd argument produces 7 because it is an array returned from the search query but when it is added to the myData array collection, it became a single object. But that object really contained the values when I checked it by accessing them like arrays (3rd argument). My problem is, why is it that the List is just displaying 1 row and it says, "[object DataList]". Is there a way for the List to display or access the myData like I did on my 3rd argument that it outputs the real values?

                           

                          Thanks Dave.

                           

                           

                          [update]

                           

                               I already got it but with a datagrid. I don't know why it isn't working on a list component. Here's the code snippet:

                           

                          for each (var item:Object in getBarangaysByNameResult.lastResult)
                          {
                               myData.addItem({id:item.barangay_id, 
                                             name:item.barangay_name});
                          }
                          

                           

                           

                          Thank you so much David.

                           

                           

                          [update]

                               It works with List now. I just forgot to place the labelField property. hehe Thanks..