Flex cairngorm overview and tutorial part2

This is a continuation for what I had explained here. If the flex application has to make calls to the external services, this can be done through the cairngorm service locator. A delegate class will be defined for each command class which would make such external http service calls. a sample code for the service locator and delegate is defined below.

<?xml version="1.0" encoding="utf-8"?>
<cairngorm:ServiceLocator
	xmlns:mx="http://www.adobe.com/2006/mxml" 
	xmlns:mxsrv="mx.rpc.http.*" 
	xmlns:cairngorm="http://www.adobe.com/2006/cairngorm">

<mx:HTTPService id="getAppService" url="http://hubpages.com/hub/flex-cairngorm-overview-and-tutorial" makeObjectsBindable="false" resultFormat="object" />

</cairngorm:ServiceLocator>

The service locator should also be included in the application.mxml file as below

<business:Services id="services" />

Creating the delegate class

When a command has to be a call to the external service. it is handled through a delegate class which would make the service call and forward the response to a IResponder. usually the the command class would only implement the IResponder interface also.

public class GetAppCommand implements ICommand, IResponder
{
 public function execute(event:CairngormEvent):void {
 	var delegate : AppInfoDelegate = new AppInfoDelegate( this );
	delegate.getAppService();
	}
}
public class AppInfoDelegate{
public function AppInfoDelegate( command : IResponder ) {			
	var qn : QName = new QName(ServiceLocator.getInstance().getHTTPService( 'getAppService' ).url, "app");
	// constructor will store a reference to the service 
	this.service = ServiceLocator.getInstance().getHTTPService( 'getAppService' );
	// and store a reference to the command 
	this.command = command;
}

    public function getAppConfigurationService() : void {
      var token:AsyncToken = service.send();
	// notify this command when the service call completes
      token.addResponder( command );
	}
}

The delegate has added the responder to the token received from the service call. It would call the result() method defined in the IResponder. The responder would handle the response and then dispatch another cairngorm event which would be handled by another command.

More by this Author


Comments

No comments yet.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working