tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/1084-how-to-service-result-disambiguationRobotlegs: Discussion 2018-10-18T16:35:43Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/189868712012-09-24T08:52:03Z2012-09-24T08:55:34Z[How to] Service result disambiguation<div><p>Hey all (and sorry for the mysterious thread subject),<br>
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!</p>
<p>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.</p>
<p>Here's how it looks (simplified):</p>
<pre>
<code>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();
}
}</code>
</pre>
<p>Now, this looks simple.</p>
<p>But what bugs me is how to deal with service classes that
provide more than one method.<br>
How do you differentiate called functions in the result
handler?</p>
<p>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...</p>
<p>What do you think?<br>
How do you deal with that?<br>
I'm tempted to write a service class per remote method, but I'm
sure there's a better way to handle this thing!</p>
<p>Thanks!</p></div>Quentintag:robotlegs.tenderapp.com,2009-10-18:Comment/189868712012-09-24T10:10:09Z2012-09-24T10:10:09Z[How to] Service result disambiguation<div><p>WOW, OK!<br>
So I just realized (after many years of Flex+AMFPHP) that you can
actually add events listeners on a specific remote method.</p>
<p>Like so:</p>
<pre>
<code>_ro.authenticate.addEventListener(ResultEvent.RESULT, onAuthenticateResult);
_ro.authenticate(credentials.login, credentials.password);</code>
</pre>
<p>I can't believe I didn't know that.<br>
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.</p>
<p>Stupid and happy.</p></div>Quentin