2012-04-19 2 views
0

배열을 채 웁니다.AS3 dispatchEvent for forEach

해당 배열의 각 항목을 가져 와서 해당 항목에 대한 계산을 수행하여 결과를 배열로 푸시 한 다음 배열의 다음 항목으로 이동해야합니다.

계산은 별도의 클래스로 이루어집니다. 그런 다음 계산이 완료되면 이벤트를 보내고 완료 할 클래스를 수신합니다.

forEach를 사용하고 있지만 forEach 함수를 일시 중지하고 계속하기 전에 dispatchEvent 수신기를 기다려야하지만 작동하지 않는 것 같습니다.

추적은 forEach가 배열을 통해 실행 중이고 매번 계산 클래스를 다시 설정하고 있음을 나타냅니다.

여기 내 코드의 jist입니다. 내 서버의 SQL 테이블에서 배열을 채운 다음 forEach를 시작합니다.

사람은 해결책을하시기 바랍니다 제안 할 수 있습니다 :

function handleLoadSuccessful(evt:Event):void 
    { 
     evt.target.dataFormat = URLLoaderDataFormat.TEXT; 
     var corrected:String = evt.target.data; 
     corrected = corrected.slice(1,corrected.length-1); 
     var result:URLVariables = new URLVariables(corrected); 
     if (result.errorcode=="0") 
     { 
      for (var i:Number=0; i < result.n; i++) 
      { 
       liveOrderArray.push(
       { 
        code:result["ItemCode"+i], 
       qty:Number(result["LineQuantity"+i]) - Number(result["DespatchReceiptQuantity"+i]) 
       })      
      }  
      liveOrderArray.forEach(allocate); 
     } else { 
      trace("ERROR IN RUNNING QUERY"); 
      } 
    }   
    function allocate(element:*, index:int, arr:Array):void {     
       trace("code: " + element.code + " qty:" + element.qty); 
       allocationbible.profileCode = element.code.substring(0,1); 
       allocationbible.finishThk = Number(element.code.substring(1,3)); 
       allocationbible.longEdgeCode = element.code.substring(3,4); 
       allocationbible.backingDetailCode = element.code.substring(4,5); 
       allocationbible.coreboardCode = element.code.substring(5,6); 
       allocationBible = new allocationbible; 
       allocationBible.addEventListener("allocated", updateAllocationQty, false, 0, true); 
       trace("*************************************"); 
      } 
function updateAllocationQty (evt:Event):void {     
       //add result to array     
       trace(allocationbible.coreboardLongCode);    
      } 

답변

0

당신이 다음 파견 이벤트를 완료하는 기능을 기다릴 스크립트의 실행을 중지해야 할 경우 당신이 그것을 할 방법이 아니다. 당신이 원하는 것은 당신이 기다리는 값을 리턴하고 이벤트를 전혀 사용하지 않는 것입니다. 난 당신이 당신이 for..each 루프를 일시 중지 할 수 없습니다 allocationbible

+0

element.code는 30 자리 코드가 다른 부분 문자열이 다른 매개 변수입니다. 이러한 매개 변수를 기반으로 원자재에 대한 생산량을 계산하고이 원자재가 allocationbible을 계산합니다. forEach를 사용하는 것보다 다른 경로가 있습니까? 할당을 계산하는 데 필요한 정보를 얻기 위해 dispatchEvent를 사용하는 여러 SQL 테이블을 참조합니다. – user1344454

0

에서 무엇을하고 있는지 알고 있다면

아마 더 많은 도움을 줄 수, 그것은 AS3에서 할 수 없습니다. 따라서 코드를 다시 작성해야합니다.

UPD : 지난 번 질문에 대한 오해. 추후 처리를하기 전에 계산이 완료 될 때까지 기다리면 할당 이벤트 핸들러에서 다음 요소의 처리를 시작할 수 있습니다. 이 같은 것 :

var currentItemIndex:int; 

function startProcessing():void { 
     // population of the array 
     // ... 
     allocationBible = new allocationbible(); 
     allocationBible.addEventListener("allocated", onAllocated); 

     currentItemIndex = 0; 
     allocate(); 
} 

function allocate():void { 
     var element:* = liveOrderArray[currentItemIndex]; 
     // configure allocationBible 
     allocationBible.process(element); 
} 

function onAllocated(e:Event):void { 
     trace("Allocated: " + allocationbible.coreboardLongCode); 

     // allocate the next item 
     currentItemIndex++; 
     if (currentItemIndex >= liveOrderArray.length) { 
      allocationBible.removeEventListener("allocated", onAllocated); 
     } else { 
      allocate(); 
     } 
}