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:
-
//ONE OPTION FOR DECLARING RESPONDER METHODS:
-
public class SomeDelegate implements IResponder
-
{
-
public function doSomething(){
-
var token:AsyncToken = this.service.send( {//send in stuff} );
-
token.addResponder(this); //YOU CAN DO THIS BECAUSE CLASS IMPLEMENTS IRESPONDER INTERFACE, AND SO RESULT/FAULT WILL GET CALLED
-
}
-
-
public function result(obj:Object):void{ //do something if server request is successful }
-
public function fault(obj:Object):void{ //do something if server request fails }
-
}
-
-
//=====================================================================
-
-
//ANOTHER OPTION FOR DECLARING RESPONDER METHODS:
-
public class SomeDelegate
-
{
-
public function doSomething(){
-
var token:AsyncToken = this.service.send( {//send in stuff} );
-
var responder:mx.rpc.Responder = new mx.rpc.Responder( loginResult, loginFault );
-
token.addResponder(responder); //CLASS DOESN'T IMPLEMENT INTERFACE, SO YOU NEED THE LINE ABOVE
-
}
-
-
public function loginResult(obj:Object):void{ //do something if server request is successful }
-
public function loginFault(obj:Object):void{ //do something if server request fails }
-
}
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:
-
//Constructor function for class LoginDelegate
-
public function LoginDelegate( responder : IResponder )
-
{
-
this.service = ServiceLocator.getInstance().getHTTPService( "aaa" ); //note: getHTTPService specifically called
-
this.responder = responder; //the responder is a reference to your Command class
-
}
-
-
public function login( loginVO : LoginVO ): void
-
{
-
var token:AsyncToken = this.service.send( {username:loginVO.username, password:loginVO.password} );
-
token.addResponder(this); //the LoginDelegate class implements mx.rpc.IResponder and so it will declare "result" and "fault" methods
-
}
In Services.mxml you need to declare your HTTPService. Note how the id corresponds to the above ServiceLocator call.
-
<!-- HTTPService declared in Services.mxml -->
-
<mx:httpservice id="aaa">
-
url="http://someurl/login.php"
-
showBusyCursor="true"
-
method="POST"
-
resultFormat="text"
-
/>
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).
-
//Constructor function for class LoginDelegate
-
public function LoginDelegate( responder : IResponder )
-
{
-
this.service = ServiceLocator.getInstance().getWebService("bbb"); //note: getWebService specifically called
-
this.responder = responder; //the responder is a reference to your Command class
-
}
-
-
public function login( loginVO : LoginVO ): void
-
{
-
var token:AsyncToken = this.service.doLogin(loginVO);
-
token.addResponder(this); //the LoginDelegate class implements mx.rpc.IResponder and so it will declare "result" and "fault" methods
-
}
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:
-
<!-- WebService declared in Services.mxml -->
-
<mx:webservice>
-
id="bbb"
-
wsdl="{model.config.someWsdlUrl}"
-
useProxy="false"
-
>
-
<mx:operation concurrency="multiple" name="doLogin" resultformat="e4x" />
-
<mx:operation concurrency="multiple" name="doSomethingElse" resultformat="e4x" />
-
</mx:webservice>
11 Comments so far
Leave a reply
thank you very much about this accurated and well explained post dude! I am struggling with cairngorm here and your help was VERY…helpfull :P
take care (blog added)
dude… this is great…. i just got saved a lot of my time because of this.. i was so stuck with this iresponder attaching to token. :) i just got it solved.. thank you very much.
[...] take a look at http://nwebb.co.uk/blog/?p=118 [...]
Hello
I would have a question. When you do a httpservice with cairngorm, and that you want to execute a function that modify a component on your mxml file AFTER the httpservice how can you do ?
Often in a mxml, the function that modify a component is executed after the httpservice…
Thanks
@Kire - if I understand you correctly you want to modify a component after you have retrieved the results from an HTTPService call? If so, the general flow with Cairngorm is that in the resultHandler of your HTTPService, you update the model with the info you’ve retrieved. Your view components are bound to the model, so the view updates itslef when the model changes. If you prefer you can use ChangeWatcher directly to trigger methods when values in the model change.
[...] Neil Webb :: Cairngorm Webservice & HTTPService Examples [...]
[...] Neil Webb :: Cairngorm Webservice & HTTPService Examples [...]
[...] Neil Webb :: Cairngorm Webservice & HTTPService Examples [...]
[...] Neil Webb :: Cairngorm Webservice & HTTPService Examples [...]
I am making the assumption that
model.config.someWsdlUrl
is a static public var? so this example does not use the Data,Import Web Service option? If it does, I am not doing a good job of following!
I am very new!
Do you know that the MXML examples you are showing are using lowercase for the class names… e.g. <mx:httpservice and <mx:webservice … which will cause compilation errors.