找出在Parsley Dynamic Command中发送请求的消息类型

时间:2013-03-12 09:55:15

标签: flex parsley

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

以下是示例代码

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;
    }
}

RequestObject类

 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类的结果方法中识别调用该服务的消息类型,该方法将在服务响应到达时被调用。

1 个答案:

答案 0 :(得分: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);
}