6 Replies Latest reply on Sep 14, 2010 5:11 AM by Deh Bontempos

    call responder within a loop. How to get those info?

    Deh Bontempos

      whats the method to retrieve information like this?

       

      The idea is to retrieve a information on data base based on the item's id.

       

      private var ids_ac:arrayCollection = new ArrayCollection([{id:4},{id:5},{id:6}]);  //these are the ids i have to retrieve info private var ids_results:array = new Array(); for ( var i:String in ids_ac ) {      getProcessedResult.token = Service.getProcessed( ids_ac[ i ].id );         //this returns an integer.      getProcessedResult.addEventListener(ResultEvent.RESULT,onResult);      function onResult(e:ResultEvent):void      {           ids_results.push(e.result.data)  // only the last id results are stored because the onResult function comes after all the looping is finished.      } }

      while debugging I saw the all the looping is performed before the onResult take place. This way, one result is overrider the earlier result.

      is there any method I can do this?

       

      Thanks in advance!

       

      Btp~

        • 1. Re: call responder within a loop. How to get those info?
          jfb00 Level 3

          Hi ,

          You don't have to do a loop.

          When you call your method, the result can be add in your array collection like:

           

           

          private var ids_ac:arrayCollection = new ArrayCollection();

           

          function onResult(e:ResultEvent):void      { 

               ids_ac = ArrayCollection(e.result);

          }

           

          If you want to add items to your array collection then use ids_ac.addItem( );

          Also after adding an item don't forget to use ids_ac.refresh();

          Rgds

           

          Johnny

          • 2. Re: call responder within a loop. How to get those info?
            Gregory Lafrance Level 6

            Hard to read your code post. You may want to format it better so we can read it easier.

            • 3. Re: call responder within a loop. How to get those info?
              Deh Bontempos Level 1

              Hi,

               

              thanks for the reply. The code was well formated while I posted. For some reasons, I should not use the "html" formater.

               

              private var ids_ac:arrayCollection = new  ArrayCollection([{id:4},{id:5},{id:6}]);  //these are the ids i have to  retrieve info

               

              private var ids_results:array = new Array();

               

              for ( var  i:String in ids_ac )

              {

                   getProcessedResult.token =  Service.getProcessed( ids_ac[ i ].id );         //this returns an  integer. 

                   getProcessedResult.addEventListener(ResultEvent.RESULT,onResult);

                    function onResult(e:ResultEvent):void

                   {

                         ids_results.push(e.result.data)  // only the last id results are stored  because the onResult function comes after all the looping is finished.

                   }

              }

               

               

              My workaround is to make a ping pong btw 2 functions:

               

               

              private var function ids_results():void

              {

                   var ck:int = array.lenght; //number of ids to check;

                   var i:int = 0;

                   go();

                   function go():void

                   {

                       if ( i < ck )

                       {

                             getProcessedResult.token =  Service.getProcessed( ids_ac[ i ].id );

                             getProcessedResult.addEventListener(ResultEvent.RESULT,onResult);

                       }

                        else

                        {

                             // end of function...fill the dataprovider or something

                        }

                   }

                   function onResult(e:ResultEvent):void

                   {

                        ids_results.push(e.result.data);

                         i++;

                         go(); //call function again AFTER fetch the result (processed) from previous ID.

                    }

              }

               

               

              Just to be clear, I dont mean to getAllItems. I mean that I have those items already and I want to process each one by its ID.

              the "getProcessed" is a method that returns an int based on the id.

               

               

              Btp~

              • 5. Re: call responder within a loop. How to get those info?
                Radhakrishna Bhat Level 3

                Another way is to instantiate new CallResponder in 'for' loop. This will make OnResult to be called for every result returned.

                 

                for ( var  i:String in ids_ac )

                {

                     getProcessedResult = new CallResponder();

                     getProcessedResult.token =  Service.getProcessed( ids_ac[ i ].id );         //this returns an  integer.

                     getProcessedResult.addEventListener(ResultEvent.RESULT,onResult);

                      function onResult(e:ResultEvent):void

                     {

                           ids_results.push(e.result.data)  // only the last id results are stored  because the onResult function comes after all the looping is finished.

                     }

                }

                 

                 

                -Radhakrishna

                • 6. Re: call responder within a loop. How to get those info?
                  Deh Bontempos Level 1

                  This looks like a more robust approach.

                  Thanks so much for the code, Radhakrishna.

                  Thanks for those links, Jonny. They weren`t exactly what i needed here but they were useful still.

                   

                  Wish you all the best.

                   

                  Btp~