swiz帮助你调用RPC和URLserivce
RPC通过RemoteObject或HTTPService进行服务调用,用AsyncToken(继承IResponder接口)接收返回值。IResponder接口包含一个result 和fault函数。服务调用通过由继承AbstractController类的控制类完成。
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("");
// 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
}
}
}
engine类后面隐藏了一个DynamicResponder调用RPC和DynamicURLLoader调用URLRequests。
另一个常用方法是将参数传给result函数.
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;
}
}
}
参考文献:
1.
阅读(1226) | 评论(0) | 转发(0) |