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

工具类: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 + "/";
			}
		}
		
	}
}

 

转载于:https://my.oschina.net/yonguil/blog/159758

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值