Skip to content

Service Layer

Swiz helps you to invoke RPC and URL service calls.

RPC service calls with RemoteObject or HTTPService return an AsyncToken where responders can be added which have to implement the IResponder interface with a result and fault function.
Service calls in general should be invoked from a controller class which should extends the AbstractController class.

RPC

package example.ctrl
{
	import mx.rpc.AsyncToken;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
 
	import org.swizframework.controller.AbstractController;
 
	public class MyController extends AbstractController
	{
 
		[Autowire]
		public var delegate:IMyDelegate;
 
		public function MyController()
		{
			super();
		}
 
		[Mediate(event="loadSomething")]
		public function loadSomething():void
		{
			var call:AsyncToken = delegate.loadSomething();
			// faultHandler is optional
			executeServiceCall(call, resultHandler, faultHandler);
		}
 
		protected function resultHandler(re:ResultEvent):void
		{
			// TODO handle result
		}
 
		protected function faultHandler(fe:FaultEvent):void
		{
			// TODO handle fault
		}
	}
}

URLRequest/URLLoader

package example.ctrl
{
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
 
	import org.swizframework.controller.AbstractController;
 
	public class MyController extends AbstractController
	{
 
		public function MyController()
		{
			super();
		}
 
		[Mediate(event="loadSomething")]
		public function loadSomething():void
		{
			var request:URLRequest = new URLRequest("http://domain.com/api/something");
			// faultHandler, progressHandler, httpStatusHandler are optional
			executeURLRequest(request, resultHandler, faultHandler, progressHandler, httpStatusHandler);
		}
 
		protected function resultHandler(e:Event):void
		{
			// currentTarget is the URLLoader
			var result:String = e.currentTarget.data;
		}
 
		protected function faultHandler(e:Event):void
		{
			// a fault event can be IOErrorEvent or SecurityErrorEvent
			if(e is IOErrorEvent)
			{
				// TODO handle IOError
			}
			else
			{
				// TODO handle SecurityError
			}
		}
 
		protected function progressHandler(e:ProgressEvent):void
		{
			// TODO handle progress
		}
 
		protected function httpStatusHandler(e:HTTPStatusEvent):void
		{
			// TODO handle http status
		}
	}
}

The engine/class behind is called DynamicResponder fpr RPC calls and DynamicURLLoader for URLRequests.
Another common use case is passing arguments to the result function:

package example.ctrl
{
	import mx.rpc.AsyncToken;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
 
	import org.swizframework.controller.AbstractController;
 
	public class MyController extends AbstractController
	{
 
		[Autowire]
		public var delegate:IMyDelegate;
 
		public function MyController()
		{
			super();
		}
 
		[Mediate(event="SomeEvent.FOO", properties="user")]
		public function loadSomething(user:User):void
		{
			var call:AsyncToken = delegate.loadSomething(user.id);
			// faultHandler is optional
			executeServiceCall(call, resultHandler, null, [user]);
		}
 
		protected function resultHandler(re:ResultEvent, user:User):void
		{
			user.something = re.result as Something;
		}
	}
}

{ 4 } Comments

  1. JFC | August 11, 2009 at 9:42 pm | Permalink

    Hi,

    I wanted to try the second example but I think that the 0.62 version doesn’t include the executeURLRequest() method of the AbstractController class.

    Is it possible to download a compiled library with the current changes included ? (Need it for Flex Builder 4.0).

    Thanks.

  2. Sönke Rohde | August 12, 2009 at 9:37 am | Permalink

    This is currently only in the Subversion repository.
    I would really recommend trying to check out the source code on your own and to compile the swc on your machine.

  3. Max | November 14, 2009 at 9:37 pm | Permalink

    Hi,
    I use swiz 0.6.4 and flex 3.4 for a while now. It’s the first time that I have this problem:

    service result hanlder functions get called twice:

    [Mediate(event="LOGIN")]
    public function loginHandler( event:AuthEvent ):void
    {
    executeServiceCall( serviceImpl.login( event.name, event.password ), loginResultHandler, null, ["hi"] );
    }
    private function loginResultHandler( event:ResultEvent, …rest ):void
    {
    trace(rest);
    }

    this produces the output:
    hi
    [ResultEvent messageId="75141B67-0E11-7FAA-A416-F42E08F879A9" type="result" bubbles=false cancelable=true eventPhase=2],hi

    so the second time the result handler is called, the …rest – array contains a string representation of the event and the “hi”

    do you have any idea with this?

    thanks in advance,

    Max

  4. Brian Kotek | November 23, 2009 at 5:00 pm | Permalink

    For help, please post to the Google Group. The comments here are for feedback on the content itself.