Handling multiple remote services with RemoteObject – the easy way!
Ok, so you’re developing a Flex application with a bit of server-side integration (using PHP, Java, Ruby, .NET, etc) and your application is getting a little intense; multiple service calls to the server, custom logic for success and failure of said calls and everything in between.
You could use the following syntax for handling mutliple method-calls from a remote script (in this example MyService):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <mx:RemoteObject id="service" destination="amfphp" source="MyService" makeObjectsBindable="true"> <mx:method name="doSomething" result="doSomethingResult(event)" fault="doSomethingFault(event)"/> <mx:method name="doAnotherSomething" result="doAnotherSomethingResult(event)" fault="doAnotherSomethingFault(event)"/> </mx:RemoteObject> <mx:Script> <![CDATA[ private function doSomethingResult(event:ResultEvent):void { // custom logic } private function doSomethingFault(event:FaultEvent):void { // custom logic } private function doAnotherSomethingResult(event:ResultEvent):void { // custom logic } private function doAnotherSomethingFault(event:FaultEvent):void { // custom logic } ]]> </mx:Script> |
I dunno about you, but to have to define special tags in MXML for each bloody method in your script is too much effort (and can leave you with some nasty/tricky bugs if you forget to add a new tag after adding a new method in your server-side script!). I used the following mechanism in my post about integrating CodeIgniter, Flex and PHP if i remember correctly… Give me a break… I was too lazy to check (it’s midnight and i’m sleepless).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | <mx:RemoteObject id="service" destination="amfphp" source="MyService" makeObjectsBindable="true" result="resultHandler(event)" fault="faultHandler(event)"/> <mx:Script> <![CDATA[ private function resultHandler(event:ResultEvent):void { var message:RemotingMessage = event.token.message as RemotingMessage; switch(message.operation) { case "doSomething": // custom logic break; case "doAnotherSomething": // custom logic break; default: trace("Shit... Missed this one... " + message.operation); break; } } private function faultHandler(event:FaultEvent):void { var message:RemotingMessage = event.token.message as RemotingMessage; switch(message.operation) { case "doSomething": // custom logic break; case "doAnotherSomething": // custom logic break; default: trace("Shit... Missed this one... " + message.operation); break; } } ]]> </mx:Script> |
Seems much easier, doesn’t it? Essentially all you’re doing is examining the event parameter of the ResultEvent or FaultEvent, finding which method was just called and acting accordingly… This will work with any number of method calls, from any number of different RemoteObjects. You can point them all to the same handlers!
If you guys have any alternative ways of doing this, let me know!
| Print article | This entry was posted by Danny Kopping on May 6, 2009 at 12:16 am, and is filed under ActionScript 3.0, Flex, PHP. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 11 months ago
I’ve tried various methods but what you’re doing here works perfectly for what I need it for – nice work!
about 4 months ago
Damn, I should have scrolled down when I opened this link, lol. Yea, you have the right idea. Nice.
about 2 months ago
Perfect. That’s what I was looking for. Thanks.
about 1 month ago
Great! Glad you found what you needed
about 1 week ago
Great! The best way I have ever seen and easy to implement!
about 11 months ago
Thanks jassa!