tags:

views:

33

answers:

2

I have a for loop;

for (var i:int=0; i < 10; i++) 
{
        callJava.method(i);
        callJava.addEventListener("result", finalMethod);

        if (finallMethod) == "VAL: 1"; //validation failled, break the loop
}

private function finalMethod(e:ResultEvent):String
{
   return "VAL: " + e.currentTarget.lastResult;
}

Now my problems is that in my loop I need to invoke a Java method and get its result, and then continue with the next value in my loop.

How can I handle this case in flex, as my call/result is asynchronous and I will get it few milliseconds later, by the time I get the result for the first value my loop will already have completed.

EDIT;

let me add some more info on the issue, the loop is called to validate rows in a datagrid. I am fetching each row and the performing a check using a Java method, if the test passes, I need to continue to take the next row and so on. There is no point to waste time on looping thru 10 rows if the validation falls on the first row and so on.

If anyone has a better/working approach it will be appreciated.

A: 

Check out this tutorial.

thelost
@thelost can't see any data related to of having an "addEventListener" in a loop, did I miss maybe something?
Adnan
you haven't got any, but your async method call should raise an event when the result is available event you need to listen for.
thelost
+1  A: 

Calling validateRows(0, 10) will validate all rows from 0 to 9:

public function validateRows(rowNumber : int, rowCount : int) : void 
{
    callJava.method(rowNumber);

    var validationHandler : Function = 
        function(e:ResultEvent) 
        {
            callJava.removeEventListener("result", validationHandler);

            if (e.currentTarget.lastResult == "1")
            {
                dispatchEvent(new Event("RowValidationFailedEvent"));
            }
            else if (rowNumber >= rowCount - 1)
            {
                dispatchEvent(new Event("RowValidationFinished"));
            }
            else
            {
                validateRows(rowNumber + 1, rowCount);
            }
        };

    callJava.addEventListener("result", validationHandler);

}

Vladimir Grigorov