Search code examples
actionscript-3apache-flexmodel-view-controllerparsley

Parsley Command Decoupled Result Handlers and Observers


I posted a question last night that after reading back sounded awful, so I deleted it and have come back to try again, this time properly.

I have a Flex Mobile App, that uses Parsley, everything works as expected but I am trying to do use a decoupled result handler in my controller, but it is not firing when I expect it to, so would like a pointer as to why.

The command look like this:

public function execute():void
    {
        var asyncToken:AsyncToken = Db.Instance.ViewChildren(mainModel.loggedInUser.userId);
        asyncToken.addResponder(new Responder(result, error));
    }

    public function result(result:ResultEvent):void
    {
        callback(result.result);
    }

    public function error(event:FaultEvent):void
    {
        callback(event.fault);
    }

Which works as expected, the command is executed and the result handler handles the result, the problem comes when I try to put a message handler in the controller for the view.

[CommandResult]
        public function handleResult(result:AsyncToken):void
        {
            trace("result in the controller");
        }
        [CommandError]
        public function handleError(fault:AsyncToken):void
        {
            trace('error: ' + fault.fault.faultDetail);
        }

Neither of these listeners fire when a result arrives, so I did the obvious thing and changed the code to:

[CommandResult]
        public function handleResult():void
        {
            trace("result in the controller");
        }
        [CommandError]
        public function handleError():void
        {
            trace('fault in controller);
        }

Now it fires, but I have no data handle.

I did think of changing the commands execute method to

public function execute():AsyncToken
{
return Db.Instance.ViewChildren(mainModel.loggedInUser.userId);
}

as after all it does return an AsyncToken, but then the command doesn't fire at all (it is part of a 2 command sequence that is mapped to an event called ChildEvent, this is the second and last event in the chain.

So in summary, I want the above to work, but want to be able to manage the result in the decoupled result handler, but I can't work out how, the parsley manual is great for getting to this point (http://www.spicefactory.org/parsley/docs/3.0/manual/?page=commands&section=intro), but the finer details are a little sketchy.

Thanks


Solution

  • With a small tweak to the Controller code, we end up with this:

            [CommandResult(type="view.user.UserEvent", selector="loadUser")]
            public function handleResult(result:Object):void
            {
                trace("this is the command result");
            }
    

    OR

            [CommandResult(selector="loadUser")]
            public function handleResult(result:Object, trigger:UserEvent):void
            {
                trace("this is the command result");
            }
    

    Now this fires, I get an Object with my data in, resolved.

    It would be useful to note that the manual for Parsley 3.0 misses out the section that explains how this actually works. I eventually found it in the Parsley 2.2 manual (the equivalent section in the 3.0 manual has been removed!) But if you ever need it http://www.spicefactory.org/parsley/docs/2.2/manual/messaging.php#command_methods

    Thanks everyone!