【问题标题】:ActionScript 3 AsyncToken implementationActionScript 3 AsyncToken 实现
【发布时间】:2009-06-27 13:48:17
【问题描述】:

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

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

【问题讨论】:

    标签: actionscript-3 asynctoken


    【解决方案1】:

    实现一个返回 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,当响应者除了结果或故障对象。

    【讨论】:

      【解决方案2】:

      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-Simulating-Server-Calls-with-a-mock-AsyncToken

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-03
        相关资源
        最近更新 更多