如何在 ActionScript 3 中伪造 AsyncToken 返回

发布于 2024-08-31 20:36:50 字数 236 浏览 5 评论 0原文

使用欧芹, 我有一项通过以下方式访问的服务 [命令(选择器='列表')] 公共函数 getRssFeed( msg:RssEvent ):AsyncToken { 将 service.list() 作为 AsyncToken 返回; 当

我指向“真实”RssService 时,一切都按预期工作。我的问题是当我指向“Mock”RssService 时。我不知道如何用一些虚拟数据返回来伪造 AsyncToken...有人知道如何做到这一点吗?

Using Parsley,
I have a service that I access through a
[Command(selector='list')]
public function getRssFeed( msg:RssEvent ):AsyncToken
{
return service.list() as AsyncToken;
}

when I point to the "Real" RssService, everything works as expected. My problem is when I point to the "Mock" RssService. I can't figure out how to fake a AsyncToken with some dummy data return... does anyone knows how to do this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南汐寒笙箫 2024-09-07 20:36:51

解决了…………;)

 public function list():AsyncToken

     var rssFeed:Array = [rss,rss,rss];
     var token:AsyncToken = createToken(rssFeed);
     token.addResponder(new AsyncResponder(resultHandler, null));
     return token;

  }

  private function resultHandler(event:ResultEvent, token:AsyncToken = null):void
  {
     event.token.dispatchEvent(event);   
  }


  protected function createToken(result:Object):AsyncToken
  {
     var token:AsyncToken = new AsyncToken(null);
     setTimeout(applyResult, Math.random()*500, token, result);
     return token;
  }

  private function applyResult(token:AsyncToken, result:Object):void
  {
     mx_internal:token.setResult(result);
     var event:ResultEvent = new ResultEvent(ResultEvent.RESULT, false, true, result, token);
     mx_internal:token.applyResult(event);
     trace(token);
  }

Resolved.............. ;)

 public function list():AsyncToken

     var rssFeed:Array = [rss,rss,rss];
     var token:AsyncToken = createToken(rssFeed);
     token.addResponder(new AsyncResponder(resultHandler, null));
     return token;

  }

  private function resultHandler(event:ResultEvent, token:AsyncToken = null):void
  {
     event.token.dispatchEvent(event);   
  }


  protected function createToken(result:Object):AsyncToken
  {
     var token:AsyncToken = new AsyncToken(null);
     setTimeout(applyResult, Math.random()*500, token, result);
     return token;
  }

  private function applyResult(token:AsyncToken, result:Object):void
  {
     mx_internal:token.setResult(result);
     var event:ResultEvent = new ResultEvent(ResultEvent.RESULT, false, true, result, token);
     mx_internal:token.applyResult(event);
     trace(token);
  }
轻许诺言 2024-09-07 20:36:51

使用Parsley 3.0,您可以使用spicefactory异步命令更好的选择:

public class MockCommand
{
    public var callback:Function;

    public function execute():void
    {
        var timer:Timer = new Timer(500, 1);
        timer.addEventListener(TimerEvent.TIMER_COMPLETE, timer_completeHandler);
        timer.start();
    }

    private function timer_completeHandler(event:TimerEvent):void
    {
        callback(mockResultData);
    }
}

不需要mx_internal导入。

Using Parsley 3.0, you have a better option with spicefactory asynchronous commands :

public class MockCommand
{
    public var callback:Function;

    public function execute():void
    {
        var timer:Timer = new Timer(500, 1);
        timer.addEventListener(TimerEvent.TIMER_COMPLETE, timer_completeHandler);
        timer.start();
    }

    private function timer_completeHandler(event:TimerEvent):void
    {
        callback(mockResultData);
    }
}

No need for mx_internal import.

兔小萌 2024-09-07 20:36:51

不要忘记添加:

use namespace mx_internal;

否则您将收到此异常。

[故障]异常,信息=TypeError:错误#1006:setResult不是函数。

Don't forget to add:

use namespace mx_internal;

Otherwise you will get this exception.

[Fault] exception, information=TypeError: Error #1006: setResult is not a function.

夜吻♂芭芘 2024-09-07 20:36:51

我使用某种 ServiceProxy 模式,它基本上提供了 3 种方法:

  • invokeOperation
  • mockResultInvokeOperation
  • mockFaultInvokeOperation

我通过以下方式扩展 ServiceProxy:

public class SomeService extends ServiceProxy {
    public var useMock:Boolean;

    public function someServiceCall(arg1:Type, arg2:Type, responder:IResponder):AsyncToken {
        if (useMock) {
            mockResultInvokeOperation({some fake result object}, responder);
        }

        return invokeOperation("someServiceCall", [arg1, arg2], responder);
    }

}

技术方面,我使用与您完全相同的技巧。

package com.obecto.services.proxy
{
import flash.utils.setTimeout;

import mx.core.mx_internal;
import mx.messaging.messages.RemotingMessage;
import mx.rpc.AbstractOperation;
import mx.rpc.AbstractService;
import mx.rpc.AsyncToken;
import mx.rpc.Fault;
import mx.rpc.IResponder;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

public class ServiceProxy
{
    public var service:AbstractService;

    public function invokeOperation(operationName:String, arguments:Array, responder:IResponder):AsyncToken 
    {
        var operation:AbstractOperation = service.getOperation(operationName);
        operation.arguments = arguments;

        var token:AsyncToken = operation.send();
        token.addResponder(responder);
        return token;
    }

    public function mockResultInvokeOperation(mockResult:Object, responder:IResponder):AsyncToken
    {
        var fakeMessage:RemotingMessage = new RemotingMessage();

        var token:AsyncToken = new AsyncToken(fakeMessage);
        token.addResponder(responder);

        setTimeout(
            function(e:ResultEvent = null):void 
            {
                token.mx_internal::applyResult(new ResultEvent(ResultEvent.RESULT, false, true, mockResult));
            }, 1000);

        return token;
    }

    public function mockFaultInvokeOperation(message:String, responder:IResponder):AsyncToken
    {
        var fakeMessage:RemotingMessage = new RemotingMessage();

        var token:AsyncToken = new AsyncToken(fakeMessage);
        token.addResponder(responder);

        setTimeout(
            function(e:ResultEvent = null):void 
            {
                token.mx_internal::applyFault(new FaultEvent(FaultEvent.FAULT, false, true, 
                    new Fault("MOCK_FAULT", message)));
            }, 1000);

        return token;
    }

}
}

I use some sort of a ServiceProxy-pattern which provides basically 3 methods:

  • invokeOperation
  • mockResultInvokeOperation
  • mockFaultInvokeOperation

I extend the ServiceProxy in the following way:

public class SomeService extends ServiceProxy {
    public var useMock:Boolean;

    public function someServiceCall(arg1:Type, arg2:Type, responder:IResponder):AsyncToken {
        if (useMock) {
            mockResultInvokeOperation({some fake result object}, responder);
        }

        return invokeOperation("someServiceCall", [arg1, arg2], responder);
    }

}

Technology-wise I use exactly the same trick as you.

package com.obecto.services.proxy
{
import flash.utils.setTimeout;

import mx.core.mx_internal;
import mx.messaging.messages.RemotingMessage;
import mx.rpc.AbstractOperation;
import mx.rpc.AbstractService;
import mx.rpc.AsyncToken;
import mx.rpc.Fault;
import mx.rpc.IResponder;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

public class ServiceProxy
{
    public var service:AbstractService;

    public function invokeOperation(operationName:String, arguments:Array, responder:IResponder):AsyncToken 
    {
        var operation:AbstractOperation = service.getOperation(operationName);
        operation.arguments = arguments;

        var token:AsyncToken = operation.send();
        token.addResponder(responder);
        return token;
    }

    public function mockResultInvokeOperation(mockResult:Object, responder:IResponder):AsyncToken
    {
        var fakeMessage:RemotingMessage = new RemotingMessage();

        var token:AsyncToken = new AsyncToken(fakeMessage);
        token.addResponder(responder);

        setTimeout(
            function(e:ResultEvent = null):void 
            {
                token.mx_internal::applyResult(new ResultEvent(ResultEvent.RESULT, false, true, mockResult));
            }, 1000);

        return token;
    }

    public function mockFaultInvokeOperation(message:String, responder:IResponder):AsyncToken
    {
        var fakeMessage:RemotingMessage = new RemotingMessage();

        var token:AsyncToken = new AsyncToken(fakeMessage);
        token.addResponder(responder);

        setTimeout(
            function(e:ResultEvent = null):void 
            {
                token.mx_internal::applyFault(new FaultEvent(FaultEvent.FAULT, false, true, 
                    new Fault("MOCK_FAULT", message)));
            }, 1000);

        return token;
    }

}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文