ASyncToken example

ASyncToken in Flex allows you to add additional data to your Remote Object calls. Consider the AS3 application below.

Action Script Code

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="htp://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx" 
    minWidth="955" minHeight="600"
    creationComplete="init(event)">
 
  <s:layout>
    <s:VerticalLayout />
  </s:layout>
 
  <fx:Script>
    <![CDATA[
      import Responders.BravoResponder;
 
      import mx.controls.Alert;
      import mx.events.FlexEvent;
      import mx.rpc.AsyncToken;
      import mx.rpc.events.FaultEvent;
      import mx.rpc.events.ResultEvent;
 
      [Bindable]
      private var letter : String;
 
      protected function init(event:FlexEvent):void
      {
          letter = 'A';
      }
 
      protected function button1_clickHandler(event:MouseEvent):void
      {
 
          var token : AsyncToken = ro.token_call(letter);
          token.letter = letter;
 
          token.addResponder(new mx.rpc.Responder(onResult, onFault));
 
          if(letter == 'C')
            letter = 'A';
          else if(letter == 'B')
            letter = 'C'
          else
            letter = 'B';
      }
 
      protected function onResult(event : ResultEvent) : void
      {
          var myData   : String = event.result as String;
          var myLetter : String = event.token.letter;
 
          Alert.show("Return: " + myData, "Token: " + myLetter);
      }
 
      protected function onFault(event:FaultEvent):void
      {
          Alert.show("Crap, something went wrong");
      }
 
    ]]>
  </fx:Script>
 
  <fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
    <s:RemoteObject id="ro"
                    destination="amfphp"
                    source="Bravo" />
  </fx:Declarations>
 
  <s:Label text="{letter}" />
  <s:Button label="token call"
            click="button1_clickHandler(event)" />
</s:Application>

The application displays a letter - initiated to 'A' and a button. When you press the button, the value of the letter is passed to a Remote Procedure call and the letter is incremented to the next letter. The letters run in sequence of ABC.

A difference we see in this code from my other RPC examples is here:

var token : AsyncToken = ro.token_call(letter);
token.letter = letter;
 
token.addResponder(new mx.rpc.Responder(onResult, onFault));

We are adding the result and fault events to the actual token (object returned from a RemoteObject call), rather than coding it directly in a method tag (a property of the RemoteObject declared in the declarations section). See my other RemoteObject examples where I have done this.

ASyncToken is a dynamic class. That means I can 'add' properties to it at run-time, and in this case I am adding a property called letter and assigning it the parameter I passed to the remote procedure call (ro.token_call). What this means is that when my result event handler is called, I will have access to this token and the additional properties I set for it. If you examine the Flex code you will see how I have used the token.

PHP Code
The PHP code returns a string based on the letter that was passed to it. There is also a one-to-one relationship with the input parameter and what has been returned which will be very useful for our testing.

If we press the button 3 times in rapid succession then we are invoking 3 separate remote procedure calls passing ABC. Examining the PHP code we can see that passing ABC in rapid succession (about a second apart from each button call), should respond back with BAC.

PHP Code

class Bravo
{
 
public function __construct()
{
}
 
 
public function token_call($p_letter)
{
$statement = 'default';
switch($p_letter)
  {
  case 'A' : 
    sleep(10);
    $statement = 'a bit slow is A';
    break;
 
  case 'B' : 
    sleep(5);
    $statement = 'a bit quicker is B';
    break;
 
  case 'C' :
    sleep(15);
    $statement = 'even slower is C';
    break;
  }
 
return $statement;

If we get BAC back from the server then I should expect the first Alert box to display the message a bit quicker is B with a token.letter value of 'B'. In fact that is exactly what I get as shown in the screen shot on the left.

If we had not used an ASyncToken then our result handler would have run 3 times but we would not have known which of our Flex calls had triggered that particular result handler. Using the token we were able to add additional properties which helped us identify which call the handler was responding to. An example of where an ASyncToken would be very beneficial is data paging with lists and data-grids where you are making RPC calls based on the scroller positon

Comments

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.