Over 10 years of community discussion and knowledge are maintained here as a read-only archive.
package com.me.myapp.model.request{ import org.puremvc.as3.patterns.observer.Observer; public class ServiceRequest extends Observer { public static const RESULT_OK:String = "result/ok"; public static const RESULT_FAULT:String = "result/fault"; public var hasCallback:Boolean = false; public var requestData:Object; public var resultData:Object; public function ServiceRequest( requestData:Object=null, callback:Function=null, caller:Object=null ) { super( callback, caller ); this.requestData= requestData; hasCallback = ( callback != null && caller != null ); } }}
var request:ServiceRequest = new ServiceRequest( myRequestData, handleResult, this );myServiceProxy.invokeService( request );
private var service:RemoteObject = new RemoteObject("MyService");override public function onRegister():void{ service.myMethod.addEventListener("result", result); service.addEventListener("fault", fault);}public function invokeService( request:ServiceRequest ):void{ var token:AsyncToken = service.myMethod( request.data ); token.request = request;}private function result( event:ResultEvent ):void{ var token:AsyncToken = event.token; token.request.resultData = event.result; var note:Notification = new Notification( ServiceRequest.RESULT_OK, token.request ); if ( token.request.hasCallback ) token.request.notifyObserver( note );}private function fault( event:FaultEvent ):void{ var token:AsyncToken = event.token; token.request.resultData = event.fault; var note:Notification = new Notification( ServiceRequest.RESULT_FAULT, token.request ); if ( token.request.hasCallback ) token.request.notifyObserver( note );}
private function handleResult( note:INotification ):void { var request:ServiceRequest = note.getBody() as ServiceRequest; switch ( note.getName() ) { // handle OK result here case ServiceRequest.RESULT_OK: var response:Object = request.data; // do stuff with the response. Or not... break; // handle Fault here case ServiceRequest.RESULT_FAULT: var fault:Fault = request.data as Fault; // do stuff with the fault, log or display... break; } onComplete();}
The first solution -> the model knows about an observer (request) (which knows about the command)
The second solution -> the model knows about a requestVO (which knows about the IResponder, AKA the command)