您的位置:首页 > 其它

Flex前后台交互,service层调用后台服务的简单封装

2013-09-07 09:50 169 查看
工具类:ServiceProxy.as

package
{
import mx.controls.Alert;
import mx.events.CloseEvent;
import mx.rpc.AbstractOperation;
import mx.rpc.AsyncResponder;
import mx.rpc.AsyncToken;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.remoting.RemoteObject;

/**
*flex前后台交互 service层的一个简单封装
* @author damon
*
*/
public class ServiceProxy
{
public function ServiceProxy()
{
}

public static function execute(destination:String,method:String,onResult:Function,...params:Array):void{
var service:RemoteObject = new RemoteObject(destination);
service.showBusyCursor = true;
service.endpoint = Config.path;
trace(Config.path)
var asyncToken:AsyncToken;
var operation:AbstractOperation = service.getOperation(method);
if(params && params.length != 0){
asyncToken = operation.send.apply(operation,params);
}else{
asyncToken = operation.send();
}
asyncToken.addResponder(new AsyncResponder(function(event:ResultEvent,asyncToken:AsyncToken):void{
onResult(event)
},
function(event:FaultEvent,asyncToken:AsyncToken):void{
var msg:Object = event.fault.rootCause;
if(msg && msg.hasOwnProperty("cause") && msg.cause){
msg = msg.cause.message;
}else{
msg = event.fault.faultString;
msg = msg.replace('java.lang.Exception :','');
}
trace(msg)
},asyncToken));
}
}
}

工具类 : Config.as

获取channel point的值

package
{
import mx.messaging.config.ServerConfig;
/**
*获取 channel 的 端点
* @author damon
*
*/
public class Config
{
private static var _path:String;
public function Config()
{
}

public static function get path():String{
if(Config._path){
return Config._path;
}else{
var point:String = ServerConfig.getChannel("my-amf",false).endpoint;
trace(point)
var index:int = point.indexOf("/WebRoot/messagebroker/amf");
point = point.substring(0,index);
if(point.lastIndexOf("/") != -1){
return point;
}
return point + "/";
}
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: