dataservice.as

来自「flex 实现的一个showcase 喜欢flex的朋友可以」· AS 代码 · 共 104 行

AS
104
字号
package com.teknision.net.remote.service{
	
	import flash.events.*;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	
	public class DataService{
		
///////////////////////////////////////////////////////////////////////////////
		
		public var service_config:DataServiceConfig;
		public var result:Object;
		
///////////////////////////////////////////////////////////////////////////////
		
		private var __loader:URLLoader;
		
		
///////////////////////////////////////////////////////////////////////////////
		
		
		public function DataService(i_service_config:DataServiceConfig){
			super();
			service_config=i_service_config;
		}

///////////////////////////////////////////////////////////////////////////////	

		
		public function invoke(service_vars:URLVariables):void{
		
			if(!canInvoke()){
				var config_er:Error= new Error("DataServiceConfig is not loaded, cannot proceed to load remote data");
				throw(config_er);
			}else{
				var req:URLRequest=buildRequest(service_vars);
				load(req);
			}
			
		}
		
		
		public function canInvoke():Boolean{
			var r_val:Boolean=true;
			if(service_config.requiresload && !service_config.loaded){
				r_val=false;
			}
			return r_val;
		}
		
///////////////////////////////////////////////////////////////////////////////	

		public function load(i_req:URLRequest):void{
			__loader=new URLLoader();
			configureLoader(__loader);
			
			__loader.addEventListener(Event.COMPLETE,handleDataLoad);
			__loader.addEventListener(IOErrorEvent.IO_ERROR,handleDataFailed);
			__loader.load(i_req);
		}
		
		
		
		public function buildRequest(service_vars:URLVariables):URLRequest{
			//OVERRIDE
			var req:URLRequest=new URLRequest("");
			req.data=service_vars;
			return req;
		}
		
		
		public function configureLoader(i_loader:URLLoader):void{
			//OVERRIDE
		}
		
		
		public function populateFromData(i_data:String):void{
			//OVERRIDE
			result=i_data;
		}
		
///////////////////////////////////////////////////////////////////////////////	
			
		private function handleDataLoad(event:Event):void{
			event.stopPropagation();
			populateFromData(__loader.data);
			var o_event:Event=new Event(Event.COMPLETE);
			dispatchEvent(o_event);
		}
		
		
		
		private function handleDataFailed(event:IOErrorEvent):void{
			event.stopPropagation();	
			var o_event:IOErrorEvent=new IOErrorEvent(IOErrorEvent.IO_ERROR);
			dispatchEvent(o_event);
		}



///////////////////////////////////////////////////////////////////////////////	

	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?