Asynchronus calls in Flex 2

by John Wilker in EffectiveUI

Spent much of today and yesterday bashing my skull trying to figure out my event handling problem, in the app I’m building.

Tom helped with the first part of my problem.

I was assigning a result and fault handler in each method of my AS class, to correspond to a method in my webservice. What I didn’t realize was that I was assigning multiple handlers to "result", so by the time my last method fired off, three handlers jumped in to handle the result, no bueno.

So I set about figuring out how to assign handlers just to the result of the specific function. I tried, class.method.addeventListener(); no joy. Found a reference to _Result as part of the function name to indicate it was a result handler, no joy there.

Tom was driving up the coast (who needs to work, when you’re Tom), so I had no thought bouncing board, in walks (via AIM) Rob.

I explain my conundrum, he thinks on it. Suggests using Flexs Asynchronus call functionality. Being still new to all the ins and outs of Flex, I reply, "Eh?"

He pastes some info from the Language reference, looks very much like what I want. I do my own searching in the language reference, yes, promising indeed.

A little tweaking of code. VIOLA! my web service calls are handled by the appropriate handler, actually a global handler with some logic in it. Cleaned up my AS quite nicely. Code Below.

public function personObject():void
            personWS = new WebService();
            personWS.useProxy = false;
            WSDLStatus = "nothing yet.";
            //Handlers. These are the global handlers for this webservice.
            personWS.addEventListener("result", resultHandler);
            personWS.addEventListener("fault", faultHandler);           

public function doEcho():void
            WSDLStatus = "Calling Echo.";       
            var tempAO:AbstractOperation;
            tempAO = personWS[‘echo’];
            //Define the Call obj. These lines set up my call to be marked. So when the results come back, asynchronously, I know which result I’m dealing with.
            var call:Object = tempAO.send();
            call.marker = "echo";

private function resultHandler(result:ResultEvent):void
             //result.token is my asynchronous data, which includes the marker I added to the call before sending it.
            var call:Object = result.token;
            //handle different results based on marker value, All I have to do is keep adding to my if,else statement to handle new results. Same goes with Faults.
            if (call.marker == "echo")
                    WSDLStatus = mx.utils.ObjectUtil.toString(call.marker);
                    WSDLStatus = mx.utils.ObjectUtil.toString(result.result);               
            else if …

Flex 2 really does rock.

Comments are closed.

%d bloggers like this: