使用 Parsley,我有一个服务,我通过 [Command(selector='list')] public function getRssFeed(msg:RssEvent):AsyncToken { return service.list() as AsyncToken; }
当我指向“真实”RssService 时,一切都按预期工作。我的问题是当我指向“模拟”RssService 时。我不知道如何用一些虚拟数据返回来伪造 AsyncToken ......有人知道该怎么做吗?
使用 Parsley,我有一个服务,我通过 [Command(selector='list')] public function getRssFeed(msg:RssEvent):AsyncToken { return service.list() as AsyncToken; }
当我指向“真实”RssService 时,一切都按预期工作。我的问题是当我指向“模拟”RssService 时。我不知道如何用一些虚拟数据返回来伪造 AsyncToken ......有人知道该怎么做吗?
解决.............. ;)
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);
}
使用 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 导入。
不要忘记添加:
use namespace mx_internal;
否则你会得到这个异常。
[故障] 异常,信息=TypeError:错误 #1006:setResult 不是函数。
我使用某种 ServiceProxy 模式,它基本上提供了 3 种方法:
我通过以下方式扩展 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;
}
}
}