[How to] Service result disambiguation

Quentin's Avatar

Quentin

24 Sep, 2012 08:52 AM

Hey all (and sorry for the mysterious thread subject),
This is a not a strictly Robotlegs question but I'm sure you guys an gals will have tips for me and your answers will help RL folks!

Most of my apps have service classes that interact with an AMFPHP backend (could be anything, though.) The process is simple: the service provides a method that calls an associated remote function (on a server), parses the result, and dispatches a Signal.

Here's how it looks (simplified):

public function init():void {
    _ro=new RemoteObject('amfphp');
    _ro.source='MyService';
    _ro.addEventListener(ResultEvent.RESULT, onData);
    _ro.addEventListener(FaultEvent.FAULT, onFault);
}
protected function onFault(e:FaultEvent):void {
    trace('Fault!', e);
}
protected function onData(e:ResultEvent):void {
    // (...)
    onResult(e.result);
}
public function authenticate(credentials:UserCredentials):void {
    _ro.authenticate(credentials.login, credentials.password);
}
protected function onResult(result:Object):void {
    if (result.success) {
        var details:UserDetails=new UserDetails();
        // (...)
        authenticateSucceeded.dispatch(details);
    } else {
        authenticateFailed.dispatch();
    }
}

Now, this looks simple.

But what bugs me is how to deal with service classes that provide more than one method.
How do you differentiate called functions in the result handler?

I used to have my AMFPHP methods return an object with a "method" property, holding the called method name, and a "result" property holding the actual data, I then used that "method" property to choose how to parse and manipulate the received data, but that looks odd to me and a little bit redundant. Also, this isn't always possible, because I'm not always the one writing the AMFPHP back-end...

What do you think?
How do you deal with that?
I'm tempted to write a service class per remote method, but I'm sure there's a better way to handle this thing!

Thanks!

  1. 1 Posted by Quentin on 24 Sep, 2012 10:10 AM

    Quentin's Avatar

    WOW, OK!
    So I just realized (after many years of Flex+AMFPHP) that you can actually add events listeners on a specific remote method.

    Like so:

    _ro.authenticate.addEventListener(ResultEvent.RESULT, onAuthenticateResult);
    _ro.authenticate(credentials.login, credentials.password);
    

    I can't believe I didn't know that.
    I couldn't really guess it since the RemoteObject class is dynamic, so there's no explicit API and documentation... But I feel stupid, anyway.

    Stupid and happy.

  2. Quentin closed this discussion on 24 Sep, 2012 10:10 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac