Avg. Rating 2.7

Problem

Use HTTPService in Cairngorm-driven application instead of ServiceLocator with RemoteObject.

Solution

Omit business level using Cairngorm:ServiceLocator, add HTTPservice into corresponding delegate, add manual Iresponder.result method invoke on HTTPService result handler.

Detailed explanation

In most Cairngorm-driven example applications available in Internet (see Cairngorm Store on http://www.adobe.com/devnet/flex/articles/cairngorm_pt1.html ) communication with the server is implemented via direct class mapping using business.Service.mxml with construction like following:


 

<cairngorm:ServiceLocator

xmlns:mx="http://www.adobe.com/2006/mxml"

xmlns:cairngorm="http://www.adobe.com/2006/cairngorm">

 

<mx:RemoteObject id="productService" destination="productServiceImpl"

showBusyCursor="true">

</mx:RemoteObject>

 

<mx:RemoteObject id="creditCardService" destination="creditCardServiceImpl"

showBusyCursor="true">

</mx:RemoteObject>

 

</cairngorm:ServiceLocator>


 

And what if we don't have Java, but a .Net or other backend? We have to do several changes and modifications.

First we sould start with is to deprecate cairngorm:ServiceLocator level at all. Let's imagine we have GetTreeCommand using TreeDelegate instance to communicate with some server method. This class looks like following:


 

package command

{

import business.TreeDelegate;

import event.GetTreeEvent;

 

import com.adobe.cairngorm.commands.ICommand;

import com.adobe.cairngorm.control.CairngormEvent;

 

import mx.rpc.IResponder;

import mx.rpc.events.FaultEvent;

public class GetTreeCommand implements ICommand, IResponder

{

public function GetTreeCommand()

{

}

public function execute( event : CairngormEvent ): void

{

var delegate : TreeDelegate = new TreeDelegate( this );

delegate.getTree();

}

public function result( evt : Object ) : void

{

 

var tempx:XML = XML(evt);

...

}

}

 

}

 

Here we used custom event event.GetTreeEvent extends CairngormEvent and business.TreeDelegate in what we should make main changes. In this delegate we wouldn't use ServiceLocator.getInstance().getRemoteObject( "servicename" ) construction, we will add direct HTTPService there. Delegate class will have following code:

 

package business

{

import mx.rpc.IResponder;

import mx.rpc.events.ResultEvent;

import mx.rpc.http.HTTPService;

 

public class TreeDelegate

{

public function TreeDelegate( responder : IResponder )

{

this.responder = responder;

}

public function getTree() : void

{

var service:HTTPService = new HTTPService();

service.resultFormat = HTTPService.RESULT_FORMAT_XML;

service.url = «some url here»;

service.addEventListener(ResultEvent.RESULT, onTreeObtained);

service.send();

}

private function onTreeObtained(e:ResultEvent):void{

(e.target as HTTPService).removeEventListener(ResultEvent.RESULT, onTreeObtained);

var obj:XML = XML(e.result);

responder.result(obj);

}

private var responder : IResponder;

}

 

}

 

As You can see here in delegate constructor we initialize responder variable with IResponder instance and in ResultEvent HTTPService handler just calling manually it's method result() with obtained result as parameter. You may believe me that in GetTreeCommand result() method would be invoked without any problems.

 

Report abuse

Related recipes