Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Cairngorm - resulthandling problem

Avatar

Level 1
We are using the Cairngorm framework in our Flex 2.0 app. and
have now met a problem related to the resulthandling. If I trace
the result in the onResult handler in the Command class, the traces
once the first time i call the service, two times the second, three
times the third and so on. It might be related to the code which
adds the event handler, but I'm not sure.



Have any of you met the same problem, or maby see some errors
in the code below?



Event class:



public class GetCategoriesEvent extends CairngormEvent

{

public function GetCategoriesEvent() {

super( GetCategoriesEvent.EVENT_GET_CATEGORIES );

}

public static var EVENT_GET_CATEGORIES:String =
"getCategories";

}





Command class:



public class GetCategoriesCommand implements Command,
Responder {

private var model : ModelLocator =
ModelLocator.getInstance();



public function execute( event : CairngormEvent ) : void {



var delegate : GetCategoriesDelegate = new
GetCategoriesDelegate ( this );

var getCategoriesEvent : GetCategoriesEvent =
GetCategoriesEvent( event );

delegate.getCategories();

}



public function onResult( event : * = null ) : void {

Application.application.trace("onResult: " + event.result);

}



public function onFault( event : * = null ) : void {

Application.application.trace("onFault: " + event.result);

}

}







Delegate class:



public class GetCategoriesDelegate

{

private var responder : Responder;

private var service : Object;



public function GetCategoriesDelegate ( responder :
Responder )

{

this.service = ServiceLocator.getInstance().getHTTPService(
"getCategories" ) as HTTPService;

this.responder = responder;

}



public function getCategories(): void {

service.addEventListener(ResultEvent.RESULT,
getCategories_onResult);

service.addEventListener(FaultEvent.FAULT,
getCategories_onFault);

service.send();

}



private function getCategories_onResult( event:ResultEvent
): void {

responder.onResult(event);

}



private function getCategories_onFault( event:FaultEvent ):
void {

responder.onFault();

}

}



Service:



<mx:HTTPService id="getCategories" url="
http://localhost:9081/sparing/fondtilbud/action?action=list_verdipapir_kategorier"


showBusyCursor="true"

useProxy="false" />



1 Accepted Solution

Avatar

Correct answer by
Level 3
Hi,



Do you want a new instance of GetCategoriesDelegate response
in related request. It seems like you keep adding listeners to your
service. Try



public function getCategories(): void {

var token:AsyncToken = service.send();

if (token != null)

{

token.addResponder(responder);

}

}



Make your GetCategoriesCommand implements IResponder, and has
result and fault functions



William Chan

View solution in original post

2 Replies

Avatar

Correct answer by
Level 3
Hi,



Do you want a new instance of GetCategoriesDelegate response
in related request. It seems like you keep adding listeners to your
service. Try



public function getCategories(): void {

var token:AsyncToken = service.send();

if (token != null)

{

token.addResponder(responder);

}

}



Make your GetCategoriesCommand implements IResponder, and has
result and fault functions



William Chan

Avatar

Level 1
Thanks!



I thought also it was related to the addEventListener, but
kept using it since all other examples I found on the net used the
same code. Now we have converted it to IResponder, and it works
perfect!