0

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.

ATR
  • 2,160
  • 4
  • 22
  • 43
  • That doesn't look right to me (as a RobotLegs user). If Parsely is anything like RL, the part of the Command that is called is the execute() method. All that other stuff is not going to be called from execute(you aren't calling it) and anything calling the Command as a Command won't/shouldn't know about it. – Amy Blankenship Mar 12 '13 at 14:39
  • @AmyBlankenship: As you said command shouldn't know what is calling it, it is the exact case with parsley command. What i am end up with is two separate commands for the same model. Can you please tell me why command should not know about what is calling it ? – ATR Mar 13 '13 at 03:29
  • That is not what I said. I said that the things calling the execute() method won't know anything about other methods, because if it's wired up correctly the framework only knows what's defined in the interface. But you're right that the Command should only know what is injected in. I also would not expect that Parsley would have any facility for using or knowing about a return from execute(). That's not how commands work/what they're for. – Amy Blankenship Mar 13 '13 at 12:48

1 Answers1

1

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);
}
dvdgsng
  • 1,691
  • 16
  • 27
  • I thought this initially but what my point is parsley should provide a mechanism through which we can come to know in the result method that this result is for this message type. I am looking for that thing whether or not that facility is there. – ATR Mar 13 '13 at 03:32
  • @ankur.trapasiya: `know in the result method that this result is for this message type` <- A result is not 'for a message type', it's for a _command_. The thing you're looking for does not exist and is not necessary. BTW DynamicCommand has been removed in Parsley3, you should use MapCommand. – dvdgsng Mar 13 '13 at 04:25
  • Edit: Maybe the [CommandResult] is what you're looking for, it can be used to retrieve the actual message that triggered the command along with the result. Like this: `[CommandResult] public function handleResult (user:User, message:LoginMessage) : void {` ... See Parsley documentation 7.6.2. ... otherwise have a look into the spicelib command framework documentation. – dvdgsng Mar 13 '13 at 04:36
  • I tried command result but the function never gets called. Ultimately i am following the above mentioned approach now. – ATR Mar 13 '13 at 07:56
  • Obviously. Parsley doesn't know anything about the method. If you want it called, you need to call it from within the execute() method. – Amy Blankenship Mar 13 '13 at 12:51