An AsyncToken Primer

 

Do you know what an AsyncToken is?

 

Answer 1: The first thing I ask a Flex interviewee who claims to be proficient in Flex;)

Answer 2: The official docs describe it like this: "This class provides a place to set additional or token-level data for asynchronous RPC operations. It also allows an IResponder to be attached for an individual call. The AsyncToken can be referenced in ResultEvent and FaultEvent from the token property."

 

A bit confusing if you're not familiar with the asynchronous nature of remote calls in Flex. I've run into a lot of folks who don't know about this handy class so I'll try to shed some light.

 

Whenever you make one or more external calls from your Flex/AIR application, you never know if or when those calls will return results (or faults) nor the order in which they will return. Even though you call remote objects "RO1", "RO2" and "RO3" in order, they may return in a different order. You might want different handlers for these results as well.

 

One way to keep track of the calls is to assign an AsyncToken to each call.

 

Instead of making your remote calls like this:

myRO.myRemoteMethdod( )

 

You would do this:

var token:AsyncToken = myRO.myRemoteMethdod( )

(Make sure you import mx.rpc.AsyncToken)

 

You now have a variable, "token", which represents this particular call. Now you can assign token its own result and fault handlers like this:

 

token.addResponder( myResponderClass );

 

...where the myResponderClass class implements IResponder. Implementing IResponder simply means defining two methods with the following signatures:

 

public function result(data:Object):void
public function fault(info:Object):void

 

So you can set the result and fault handlers for your remote calls at call time rather than hard coding them into your RemoteObject, HTTPService or WebService tags. This is exactly the way Cairngorm and other microarchitectures operate.

A final cool feature of AsyncToken is that it's a dynamic class, which means that you can add properties to the token when you make the remote call, then read those properties back in the result/fault handlers.

 

Give it a try and let me know what you think!

 

翻译(转)

 

你知道AsyncToken是什么吗?

 

解答一:首先,询问一个声称精通Flex的人;

解答二:官方文档是这样描述的:"这个类针对异步RPC操作提供了一个用来设置额外的或者令牌级数据的地方。它也允许一个IResponder作为一个独立调用被附加。这个AsyncToken能够在ResultEvent和FaultEvent中用token属性来引用。"

 

如果你对Flex中远程异步调用的本质不熟悉,肯定还是有点困惑的。我碰见过大量的对这个便捷类不熟悉的人,因此我试着来让它明朗一点。

 

无论何时从你的flex/AIR应用中使用多个外部调用中的一个,你都不会知道这些调用是否或者何时返回结果(或者错误),你也不会知道它们返回的顺序。即使你按照"RO1","RO2","RO3"的顺序调用远程对象,它们返回的顺序还是可能不一样。你可能也希望针对这些结果能有不同的处理。

 

一种方式就是给每一个调用分配一个异步令牌(AsyncToken )来追踪这些调用。

不要这样来进行远程调用

myRO.myRemoteMethod();

你应该这样

var token:AsyncToken=myRO.myRemoteMethod();

(确保导入了mx.rpc.AsyncToken)

现在就有了一个变量"token",代表这个单独的调用。像这样,你可以分配token自己的result和fault处理函数

token.addResponder(myResponderClass);

这里myResponderClass类实现了IResponder.实现Iresponder简单的意味着定义了如下两个方法签名:

  • public function result(data:Object):void
  • public function fault(info:Object):void

因此你可以对你的远程调用在调用时设置result和fault处理函数,而不用把硬编码放到你的远程对象,HTTPService或者WebService标签里。这也恰好就是Cairngorm和其他微体系结构的运行方式。

AsyncToken最后一点很酷的特性就是它是动态类。这意味着,当你进行远程调用时可以添加属性到token上,然后result/fault处理函数中读取这些属性。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值