nwebb

Flex, Flash, AIR

Archive for August, 2007

Cairngorm Talk ( slides & thanks )

It was great to see so many people at the LFPUG yesterday. I hope you found the Cairngorm talk useful – it’s a pretty dry topic so I hope I managed to lighten it up a little. As always, there is sure to be room for improvement, so please feel free to email me directly with your feedback (neil @t nwebb dot co dot uk) . I’d really love to hear what you liked, didn’t like and what you think I could do better.

You can download the presentation slides here and all my Cairngorm blog posts are tagged so they are easy to find ( go to the front page of my blog and click on ‘Cairngorm’ in the Categories section).
phat controller (smaller)

Cheers,
Neil

5 comments

Cairngorm Webservice & HTTPService Examples

In my previous post I uploaded an example application which used an HTTPService within Cairngorm.
Here I'll compare the HTTPService code with equivalent WebService code so you can easily see the differences (there are very few):

HTTPService
1. You locate the specific service using the ServiceLocator singleton and the id of your HTTPService.
2. When you are ready, you call the send() method of the HTTPService (i.e. service.send()) passing in any required parameters.
3. Next you declare the responder methods for that service (i.e. these are the methods which will be called on success/fail of the server request - note: they have nothing to do with this.responder which actually refers to your Command instance ... confused yet?!).

Okay, I'll do my best to clear this up......
You need responder methods so that your asynchronous operation (the call to the server) knows which methods to call depending on whether the operation was successful or not. You can declare the responder methods using the mx.rpc.Responder class, but perhaps a cleaner way is to make your Delegate class implement the IResponder interface. Both ways are shown below:

Actionscript:
  1. //ONE OPTION FOR DECLARING RESPONDER METHODS:
  2. public class SomeDelegate implements IResponder
  3. {
  4.     public function doSomething(){
  5.         var token:AsyncToken = this.service.send( {//send in stuff} );
  6.         token.addResponder(this); //YOU CAN DO THIS BECAUSE CLASS IMPLEMENTS IRESPONDER INTERFACE, AND SO RESULT/FAULT WILL GET CALLED
  7.     }
  8.  
  9.     public function result(obj:Object):void{ //do something if server request is successful  }
  10.     public function fault(obj:Object):void{ //do something if server request fails  }
  11. }
  12.  
  13. //=====================================================================
  14.  
  15. //ANOTHER OPTION FOR DECLARING RESPONDER METHODS:
  16. public class SomeDelegate
  17. {
  18.     public function doSomething(){
  19.         var token:AsyncToken = this.service.send( {//send in stuff} );
  20.         var responder:mx.rpc.Responder = new mx.rpc.Responder( loginResult, loginFault );
  21.         token.addResponder(responder); //CLASS DOESN'T IMPLEMENT INTERFACE, SO YOU NEED THE LINE ABOVE
  22.     }
  23.  
  24.     public function loginResult(obj:Object):void{ //do something if server request is successful  }
  25.     public function loginFault(obj:Object):void{ //do something if server request fails  }
  26. }

Note: If you do it the first way then both your Command class and Delegate class are implementing the IResponder interface.

Here is the example using the HTTPService:

Actionscript:
  1. //Constructor function for class LoginDelegate
  2. public function LoginDelegate( responder : IResponder )
  3. {
  4.     this.service = ServiceLocator.getInstance().getHTTPService( "aaa" ); //note: getHTTPService specifically called
  5.         this.responder = responder; //the responder is a reference to your Command class
  6. }
  7.  
  8. public function login( loginVO : LoginVO ): void
  9. {
  10.     var token:AsyncToken = this.service.send( {username:loginVO.username, password:loginVO.password} );
  11.         token.addResponder(this); //the LoginDelegate class implements mx.rpc.IResponder and so it will declare "result" and "fault" methods
  12. }

In Services.mxml you need to declare your HTTPService. Note how the id corresponds to the above ServiceLocator call.

Actionscript:
  1. <!-- HTTPService declared in Services.mxml -->
  2. <mx:httpservice id="aaa">
  3.         url="http://someurl/login.php"
  4.         showBusyCursor="true"
  5.         method="POST"
  6.         resultFormat="text"
  7.  />

WebService
1. You locate the specific service using the ServiceLocator singleton and the id of your WebService.
2. When you are ready, you call the name of an operation on the WebService (i.e. service.doLogin()) and pass in any required parameters.
3. Next you declare the responder methods for that service (see notes in above example).

Actionscript:
  1. //Constructor function for class LoginDelegate
  2. public function LoginDelegate( responder : IResponder )
  3. {
  4.     this.service = ServiceLocator.getInstance().getWebService("bbb"); //note: getWebService specifically called
  5.         this.responder = responder; //the responder is a reference to your Command class
  6. }
  7.  
  8. public function login( loginVO : LoginVO ): void
  9. {
  10.     var token:AsyncToken = this.service.doLogin(loginVO);
  11.         token.addResponder(this); //the LoginDelegate class implements mx.rpc.IResponder and so it will declare "result" and "fault" methods
  12. }

In Services.mxml you need to declare your WebService. Note how the id corresponds to the above ServiceLocator call and how the WebService has a number of operations:

Actionscript:
  1. <!-- WebService declared in Services.mxml -->
  2. <mx:webservice>
  3.     id="bbb"
  4.     wsdl="{model.config.someWsdlUrl}"
  5.     useProxy="false"
  6. >
  7.     <mx:operation concurrency="multiple" name="doLogin" resultformat="e4x" />
  8.     <mx:operation concurrency="multiple" name="doSomethingElse" resultformat="e4x" />
  9. </mx:webservice>

13 comments

Cairngorm Login (HTTPService example with PHP)

I wanted to address some of the Cairngorm-related issues people are writing to me about - they mainly involve the Service class and how to retrieve data from a WebService or HTTPService.

Here is a quick working example of the Cairngorm Login sample application which uses HTTPService with a very simple PHP script.
Right-click for the source code. I've commented it and included the php (don't overlook the readme.txt in the same folder which tells you where to put the php).
When looking at the Flex code pay special attention to the LoginDelegate.mxml and Service.as classes.

Edit: I don't seem to be able to get this working nicely with the "KLM FlashEmbed" WordPress plugin I'm using - even if I use a hard-coded url to the php (yes, I've changed it from localhost ;) ).

You can still right-click the application above to get the source.
If you want to see a working example of the application, go here (but you can't right-click it for source).

If anyone has resolved this issue before please let me know - cheers

7 comments

Next Page »

Bad Behavior has blocked 399 access attempts in the last 7 days.