0

我正在使用动态命令的欧芹框架中开发一个应用程序。

这是示例代码

public class MyCommand extends EventDispatcher
{
    [Inject]
    public var service:IService;

    [Inject(id="model")]
    public var model:TestModel;

    public function execute(message:TestMessage):AsyncToken
    {           
        return service.getResponse(message.requestObject);
    }

    public function result(data:Object):void
    {

            //Here i want to find out the response is for message type1 or message type2, that called the service.
        var result:ByteArray=data as ByteArray;

        var response:ArrayCollection=result.readObject() as ArrayCollection;

        model.data=response;

    }

    //faultCode, faultDetail, faultString, rootCause
    public function error(info:Object):void
    {                       
        dispatchEvent(new AlertEvent(AlertEvent.SHOW_ALERT,"Service Loading Error",
            "There was an error in the application"));
    }           
}

消息类

public class TestMessage
{

    public static const Type1:int=0;
    public static const Type2:int=1;

    private var _type:int;

    private var _requestObject:RequestObjectHelper;

    [Selector]
    public function get type():int
    {
        return _type;
    }

    public function set requestObject(value:RequestObjectHelper):void
    {
        _requestObject = value;
    }

    public function get requestObject():RequestObjectHelper
    {
        return _requestObject;
    }

    public function TestMessage(type:int)
    {
        _type=type;
    }
}

请求对象类

 public class RequestObjectHelper
    {
           private var _url:String;
           private var _resultFormat:String;
           private var _noCacheParam:Object;

//getters and setters
    }

消息发送代码

public function handleViewInitialized():void
{           
    var requestObject:RequestObjectHelper;
    var message:TestMessage;

    //msg1
    requestObject=new RequestObjectHelper();

    requestObject.url="url1";

    requestObject.resultFormat="amf";

    message=new TestMessage(TestMessage.Type1);

    message.requestObject=requestObject;

    dispatcher(message);

    //msg2      
    requestObject=new RequestObjectHelper();

    requestObject.url="url2";

    requestObject.resultFormat="amf";

    message=new TestMessage(TestMessage.Type2);

    message.requestObject=requestObject;

    dispatcher(message);            
}

我希望上面的代码很容易理解。我试图让它尽可能简单。我想从这段代码中得到的是如何从 MyCommand 类的结果方法中识别调用服务的消息类型,该方法将在服务响应到达时被调用。

4

1 回答 1

1

你可以让你的命令记住它被调用的类型。

private var msgType:String;

public function execute(message:TestMessage):AsyncToken
{
    msgType = message.type;
    return service.getResponse(message.requestObject);
}

public function result(data:Object):void
{
    //Here i want to find out the response is for message type1 or message type2, that called the service.
    if(msgType==TestMessage.Type1)
         trace("Type1");
    else if(msgType==TestMessage.Type2)
         trace("Type2");
    else
         trace(msgType);
}
于 2013-03-12T15:36:43.880 回答