Question

I am developing one application in parsley framework which uses Dynamic Commands.

Here is the sample code

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

Message Class

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 class

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

//getters and setters
    }

Message dispatching code

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

I hope the above code is easily understood. I have tried to keep it as simple as possible. What i want from this code is how can i identify which type of message invoked the service, from the result method of the MyCommand class which will get called when the service response will arrive.

Was it helpful?

Solution

You could make your command remember the type it was called from.

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top