views:

34

answers:

1

Hi All, I am having a situation with my actionscript/flex front end.

 for each (var sym:String in ["A","B","C"]) {

                const handler = function (data:Object):void { Alert.show(sym); }                

                asyncCallback(handler);

   }

I am expecting to have 3 Alert windows containing A, B and C. But the actual result is 3 alert windows all showing C !

I appreciate your comments. -A

+1  A: 

This one is a bit tricky. You have to wrap your handler creation inside another function.

try:

for each(var sym:String in ["A","B","C"]) {
    function createHandler(val:String):Function {
        var handler = function(data:Object):void { 
            trace(val); 
        }
        return handler;
    }
    var handler:Function = createHandler(sym);
    asyncCallback(handler);
}
sharvey