1 Reply Latest reply on Jul 6, 2011 5:21 AM by Rangora

    addEventListner keeps skipping and not excuting code

    rcjetpilot

      In my code, DivideAmount is called and while looping I call

       

      SplitMonthResult.addEventListener(ResultEvent.RESULT, DisplaySplitVolume);.  It only executes on the last record in the loop.  Any ideas on why this would skip my function?  Below is the portion of code that is failing.

       

       

       

      protected function DivideAmount_clickHandler(event:MouseEvent):void

      {

       

      ProceedDivide = 1;

      CanEdit = 0;

       

       

       

      for(var count:int = 0; count <= VolumeDelivery.length; count++)

      {

       

      SplitMonthResult.token = contract.SplitMonth

      (VolumeDelivery.getItemAt(count).pricelevel,

      VolumeDelivery.getItemAt(count).startdate,

      VolumeDelivery.getItemAt(count).enddate,

      VolumeDelivery.getItemAt(count).volume,

      VolumeDelivery.getItemAt(count).type,

      VolumeDelivery.getItemAt(count).deliverymethod

      );

       

      SplitMonthResult.addEventListener(ResultEvent.RESULT, DisplaySplitVolume);

       

      //DisplaySplit();

      }

       

      }

       

       

       

      protected function DisplaySplit():void

      {

      DisplaySplitVolume(1);

      }

       

       

      protected function DisplaySplitVolume(temp:int):void

      {

      VolumeDeliveryGrid.visible =

      false;

      VolumeDeliveryGridOutput.visible =

      true;

       

       

      for (var i:int = i; i < SplitMonthResult.lastResult.length; i++)

      {

       

       

      var pricelevel:int = SplitMonthResult.lastResult[0].PriceLevel;

       

      var startdate:String = String(SplitMonthResult.lastResult[i].StartDate);

       

      var enddate:String = String(SplitMonthResult.lastResult[i].EndDate);

       

      var volume:String = String(int(SplitMonthResult.lastResult[i].Volume));

       

      var type:String = String(SplitMonthResult.lastResult[i].Type);

       

      var deliverymethod:String = String(SplitMonthResult.lastResult[i].DeliveryMethod);

       

       

      var SplitVolume:Object = new Object;

       

      SplitVolume.pricelevel = pricelevel;

      SplitVolume.startdate = startdate;

      SplitVolume.enddate = enddate;

      SplitVolume.volume = volume;

      SplitVolume.type = type;

      SplitVolume.deliverymethod = deliverymethod;

       

      VolumeDeliveryOutput.addItem(SplitVolume);

       

      }

       

      TotalVolume();

       

      }

        • 1. Re: addEventListner keeps skipping and not excuting code
          Rangora Level 3

          SplitMonthResult is probably an object (of type CallResponder). You have only 1 object so can't expect him to execute 10 query at the same time.

           

          Since AIR does not support threads, you can't do that this way. You will have to implement the for globaly. (store your current position in a class var. After each resultEvent, you check if your current position is under the length and you send the next call and wait for the answer the same way. If you reach VolumeDelivery.length, you will then call your TotalVolume and your displaySplit in the ResultEvent.

           

          var currentPos:int;

          protected function DivideAmount_clickHandler(event:MouseEvent):void

           

          {


          currentPos = 0;


          ProceedDivide = 1;

          CanEdit = 0;

           


           

          SplitMonthResult.addEventListener(ResultEvent.RESULT, DisplaySplitVolume);


          SplitMonthResult.token = contract.SplitMonth

          (VolumeDelivery.getItemAt(currentPos).pricelevel,

          VolumeDelivery.getItemAt(currentPos).startdate,

          VolumeDelivery.getItemAt(currentPos).enddate,

          VolumeDelivery.getItemAt(currentPos).volume,

          VolumeDelivery.getItemAt(currentPos).type,

          VolumeDelivery.getItemAt(currentPos).deliverymethod);

           

          }

           

           

           

          protected function DisplaySplit():void

          {

          DisplaySplitVolume(1);

          }

           

           

          protected function DisplaySplitVolume(temp:int):void

          {

          VolumeDeliveryGrid.visible =

          false;

          VolumeDeliveryGridOutput.visible =

          true;

           

           

          var pricelevel:int = SplitMonthResult.lastResult[0].PriceLevel; // i didnt change the 0 in this case.

           

          var startdate:String = String(SplitMonthResult.lastResult[currentPos].StartDate);

           

          var enddate:String = String(SplitMonthResult.lastResult[currentPos].EndDate);

           

          var volume:String = String(int(SplitMonthResult.lastResult[currentPos].Volume));

           

          var type:String = String(SplitMonthResult.lastResult[currentPos].Type);

           

          var deliverymethod:String = String(SplitMonthResult.lastResult[currentPos].DeliveryMethod);

           

           

          var SplitVolume:Object = new Object;

           

          SplitVolume.pricelevel = pricelevel;

          SplitVolume.startdate = startdate;

          SplitVolume.enddate = enddate;

          SplitVolume.volume = volume;

          SplitVolume.type = type;

          SplitVolume.deliverymethod = deliverymethod;

           

          VolumeDeliveryOutput.addItem(SplitVolume);

           

          if (++currentPos < VolumeDelivery.length) // you used <= but I think it was an error

          SplitMonthResult.token = contract.SplitMonth

          (VolumeDelivery.getItemAt(currentPos).pricelevel,

          VolumeDelivery.getItemAt(currentPos).startdate,

          VolumeDelivery.getItemAt(currentPos).enddate,

          VolumeDelivery.getItemAt(currentPos).volume,

          VolumeDelivery.getItemAt(currentPos).type,

          VolumeDelivery.getItemAt(currentPos).deliverymethod);

          }

          else //we got all results

          {

          SplitMonthResult.removeEventListener(ResultEvent.RESULT, DisplaySplitVolume);

           

          TotalVolume();

          //DisplaySplit();

          }

          }

           

           

          It might not be perfect but you should get the idea.