Flex 내 remote, http, url 에 대한 재 패키지

6675 단어 .netxmlFlexFlash
3 가지 연결 패키지
구성 요소 코드:

package pizazz.flex4.remote{
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.remoting.RemoteObject;

	public class Conn{
		protected function connFault(event:Event):void{
			var _errInfo:String = "    : " + event.target + "
"; _errInfo += " : " + event.type + "
"; if(event is FaultEvent){ _errInfo += " : " + (event as FaultEvent).fault.faultCode + "
"; _errInfo += " : " + (event as FaultEvent).fault.faultString + "
"; _errInfo += " : " + (event as FaultEvent).fault.faultDetail; } Alert.show(_errInfo, " "); } /** * * @param address * @param result * @param config * @param fault * @return */ internal function conn(address:String, result:Function, config:Object = null, fault:Function = null):Object{ return null; } } }

package pizazz.flex4.remote{
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	import pizazz.flex4.utility.ParamUtil;

	public class RemoteConn extends Conn{
		private var _remote:RemoteObject;

		public function RemoteConn(){
			_remote = new RemoteObject();
		}

		override internal function conn(address:String, result:Function, 
				config:Object = null, fault:Function = null):Object{
			ParamUtil.setParam(_remote, config);
			_remote.destination = address;
			_remote.addEventListener(ResultEvent.RESULT, result);
			_remote.addEventListener(FaultEvent.FAULT,
				function(event:FaultEvent):void{
					if(fault != null){
						fault(event);
					}else{
						connFault(event);
					}
				}
			);
			return _remote;
		}

		public static function getRemote(destination:String, result:Function = null, 
				config:Object = null, fault:Function = null):RemoteObject{
			return new RemoteConn().conn(destination, result, config, fault) 
				as RemoteObject;
		}
	}
}

package pizazz.flex4.remote{
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	import pizazz.flex4.utility.ParamUtil;

	public class HttpConn extends Conn{
		private var _http:HTTPService;

		public function HttpConn(){
			_http = new HTTPService();
		}

		override internal function conn(address:String, result:Function, 
				config:Object = null, fault:Function = null):Object{
			ParamUtil.setParam(_http, config);
			_http.url = address;
			_http.addEventListener(ResultEvent.RESULT, result);
			_http.addEventListener(FaultEvent.FAULT,
				function(event:FaultEvent):void{
					if(fault != null){
						fault(event);
					}else{
						connFault(event);
					}
				}
			);
			return _http;
		}

		public static function getRemote(url:String, result:Function, 
				config:Object = null, fault:Function = null):HTTPService{
			return new HttpConn().conn(url, result, config, fault) 
				as HTTPService;
		}
	}
}

package pizazz.flex4.remote{
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import pizazz.flex4.utility.ParamUtil;

	public class UrlConn extends Conn{
		private var _request:URLRequest;

		public function UrlConn(){
			_request = new URLRequest();
		}

		override internal function conn(address:String, result:Function,
				config:Object = null, fault:Function = null):Object{
			_request.url = address;
			var _loader:URLLoader = new URLLoader();
			ParamUtil.setParam(_loader, config);
			_loader.addEventListener(Event.COMPLETE, result);
			_loader.addEventListener(IOErrorEvent.IO_ERROR,
				function(event:IOErrorEvent):void{
					if(fault != null){
						fault(event);
					}else{
						connFault(event);
					}
				}
			);
			_loader.load(_request);
			return _request;
		}

		public static function getRemote(url:String, result:Function, 
				config:Object = null, fault:Function = null):URLLoader{
			return new UrlConn().conn(url, result, config, fault) 
				as URLLoader;
		}
	}
}

구성 요소 실행:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark" 
			   xmlns:mx="library://ns.adobe.com/flex/mx" 
			   minWidth="955" minHeight="600">
	<s:layout>
		<s:HorizontalLayout />
	</s:layout>
	<fx:Script>
		<![CDATA[
			import mx.controls.Alert;
			import mx.rpc.events.FaultEvent;
			import mx.rpc.events.ResultEvent;
			import pizazz.flex4.remote.HttpConn;
			import pizazz.flex4.remote.RemoteConn;
			import pizazz.flex4.remote.UrlConn;

			private function remote():void{
				HttpConn.getRemote("config.xml",
					function(event:ResultEvent):void{
						//result
					}
				, {"resultFormat": "e4x"}).send({"id": 0});
				RemoteConn.getRemote("service",
					function(event:ResultEvent):void{
						//result
					}
				, null,
					function(event:FaultEvent):void{
						Alert.show("ERROR");
					}
				).getOperation("method").send({"id": 0});
				UrlConn.getRemote("http://127.0.0.1:8080/index",
					function(event:Event):void{
						
					}
				);
			}
		]]>
	</fx:Script>
	<s:Button width="100" label="  " click="remote()" />
</s:Application>

좋은 웹페이지 즐겨찾기