寻找有关如何实现返回 AsyncToken 的方法的示例或文档链接。

请注意,这不是关于使用/消耗返回 AsyncToken 的方法!我想自己写这样的方法。

有帮助吗?

解决方案

实现该返回AsyncToken的方法是简单的:

function doStuffLater():AsyncToken {
    var token:AsyncToken = new AsyncToken(null);

    // This method will randomly call the responder's 'result' or 'fault'
    // handler.
    function doStuff() {
        var response:String = (Math.random() > 0.5)? "result" : "fault";
        for each (responder:IResponder in (token.responders || [])) {
            // rememeber: this is equivilent to
            // responder.result(...) or responder.fault(...)
            responder[response]("Got a result!");
        }
    }

    setTimeout(doStuff, 1000);

    return token;
}

请注意,不能实际使用applyResultapplyFault方法,因为它们通过应答一个Event,当应答器除了结果或错误的对象。

其他提示

Swiz 框架的 TestUtil 类有一些非常酷的方法来模拟 AsyncToken 行为:

http://code.google.com/p/swizframework/source/browse/trunk/src/main/flex/org/swizframework/util/TestUtil.as

Brian Kotek 有一篇内容非常丰富的博客文章,介绍了如何使用它来使用模拟委托来模拟服务器调用:

http://www.briankotek.com/blog/index.cfm/2009/3/16/Swiz-Part-5-Simulated-Server-Calls-with-a-mock-AsyncToken

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top