package com.firemoss.swiz.rpc { import flash.events.Event; import mx.events.DynamicEvent; import mx.rpc.AbstractOperation; import mx.rpc.AsyncToken; import mx.rpc.IResponder; import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; import mx.rpc.remoting.RemoteObject; import org.swizframework.Swiz; public class RemoteMethodMediator implements IResponder { private var operation : AbstractOperation /** * When a mediated result event is dispatched, what propery name should * ResultEvent.result be mediated to on the Swiz event? Defaults to * "result" */ public var resultProperty : String = "result" /** * To bus values across the request, add them to your asyncToken then list * their names as tokenProperties (array of strings): they'll be added * as properties to the dispatched event, which can then be used in the * properties="" bit of your mediated functions. */ public var tokenProperties : Array = [] /** * When a result event is handled, what event should be dispatched via Swiz? */ public var resultEvent : String /** * When a fault event is handled, what event should be dispatched via Swiz? */ public var faultEvent : String /** * Which method on the remote object should be mediated? */ public function get methodName() : String { return _methodName } public function set methodName( value : String ) : void { _methodName = value operationInvalidated() } private var _methodName : String /** * Which remote object should be mediated? */ public function get remoteObject() : RemoteObject { return _remoteObject } public function set remoteObject( value : RemoteObject ) : void { _remoteObject = value operationInvalidated() } private var _remoteObject : RemoteObject public function RemoteMethodMediator() { } private function operationInvalidated() : void { if ( operation ) { operation.removeEventListener( ResultEvent.RESULT, result ) operation.removeEventListener( FaultEvent.FAULT, fault ) } if ( remoteObject && methodName ) { operation = remoteObject.getOperation( methodName ) operation.addEventListener( ResultEvent.RESULT, result, false, 0, true ) operation.addEventListener( FaultEvent.FAULT, fault, false, 0, true ) } } public function result(data:Object):void { var operationResultEvent : ResultEvent = data as ResultEvent if ( resultEvent ) { var swizEvent : DynamicEvent = new DynamicEvent( resultEvent ) if ( resultProperty ) { swizEvent[ resultProperty ] = operationResultEvent.result } busTokenProperties( swizEvent, operationResultEvent.token ) Swiz.dispatchEvent( swizEvent ) } } public function fault(info:Object):void { var operationFaultEvent : FaultEvent = info as FaultEvent if ( faultEvent ) { var swizEvent : DynamicEvent = new DynamicEvent( faultEvent ) swizEvent.faultEvent = operationFaultEvent swizEvent.fault = operationFaultEvent.fault busTokenProperties( swizEvent, operationFaultEvent.token ) Swiz.dispatchEvent( swizEvent ) } } private function busTokenProperties( targetEvent : Event, token : AsyncToken ) : void { if ( tokenProperties ) { for each ( var key : String in tokenProperties ) { if ( token.hasOwnProperty( key ) ) { targetEvent[ key ] = token[ key ] } } } } } }