views:

1211

answers:

2

Right now I'm trying to understand how Flex works with Java (Flex -> BlazeDS -> Java). I tried to follow THIS tutorial and everything works fine, I just don't understand why do we need to call java function this way:

<mx:Script>
    <![CDATA[
        import mx.rpc.events.FaultEvent;
        import mx.rpc.events.ResultEvent;

        // Send the message in response to a Button click.
        private function echo():void {
            var text:String = ti.text;
            remoteObject.echo(text);
        }

        // Handle the recevied message.
        private function resultHandler(event:ResultEvent):void {
            ta.text += "Server responded: "+ event.result + "\n";
        }

        // Handle a message fault.
        private function faultHandler(event:FaultEvent):void {
            ta.text += "Received fault: " + event.fault + "\n";
        }
    ]]>
</mx:Script>

Why do we need to use Event/ResultEvent in order to call Java function. Why not just to do something like this:

EchoService.echo("hi")

Thanks

+3  A: 

It is to be able to handle server lag and other anomalous conditions. If you just called the method, your UI would freeze during the server transfer time. With the callback, the UI can continue to process events until the data has been received and is ready to be viewed.

Travis Jensen
Another way to phrase is that all remote calls executed by Flash Player are done asynchronously.
cliff.meyers
A: 

Having two separate methods depending of success or fault will allow your program to react differently if the server errors in some way.

DyreSchlock