cairngormサンプルコード


最近のプロジェクトの枠組みはcairngormを採用しており、以前に研究されていたので、ずっと役に立たなかったので、淡々と忘れてしまった.簡単な入門例を出して、くだらないことを言わないで、直接コードを出します.
package com.tqcm.vo
{
	import com.adobe.cairngorm.vo.IValueObject;
	
	public class Customer implements IValueObject
	{
		public var id:int;
		public var name:String;
		public var company:String;
		public var mail:String;
	}
}
package com.tqcm.model
{
	import com.adobe.cairngorm.model.IModelLocator;
	
	[Bindable]
	public class ModelLocator implements IModelLocator
	{
		private static var modelLocator:ModelLocator;
		
		public function ModelLocator()
		{
			if(modelLocator!=null){
				throw new Error('Only one ModelLocator instance should be instantiated');
			}
		}
		
		public static function getInstance():ModelLocator{
			if(modelLocator==null){
				modelLocator = new ModelLocator();
			}
			return modelLocator;
		}

	}
}
package com.tqcm.event
{
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.tqcm.control.Control;
	import com.tqcm.vo.Customer;
	
	public class AddCustomerEvent extends CairngormEvent
	{
		public var customer:Customer;
		public function AddCustomerEvent(customer:Customer)
		{
			super(Control.ADDCUSTOMER_EVENT);
			this.customer= customer;
		}
	}
}
package com.tqcm.control
{
	import com.adobe.cairngorm.control.FrontController;
	import com.tqcm.commands.AddCustomerCommand;
	
	public class Control extends FrontController
	{
		public static const ADDCUSTOMER_EVENT:String = "addCustomerEvent"
		public function Control()
		{
			addCommand(Control.ADDCUSTOMER_EVENT,AddCustomerCommand);
		}

	}
}
package com.tqcm.commands
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.tqcm.bussiness.AddCustomerDelegate;
	import com.tqcm.event.AddCustomerEvent;
	import com.tqcm.model.ModelLocator;
	
	import mx.controls.Alert;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	public class AddCustomerCommand implements ICommand,IResponder
	{
		[Bindable]
		public var modelLocator:ModelLocator = ModelLocator.getInstance();
		public var addCustomerDelegate:AddCustomerDelegate;
		public function execute(event:CairngormEvent):void{
			addCustomerDelegate = new AddCustomerDelegate(this);
			addCustomerDelegate.execute(AddCustomerEvent(event).customer);
		}
		
		public function result(data:Object):void{
			var result:int = ResultEvent(data).result as int;
			if(result==0){
				Alert.show("success");
			}else{
				Alert.show("failed");
			}
			removeEvntListener();
		}
		
		public function fault(info:Object):void{
			Alert.show(FaultEvent(info).fault.toString());
			removeEvntListener();
		}
		
		public function removeEvntListener():void{
			addCustomerDelegate.service.removeEventListener(ResultEvent.RESULT,result);
			addCustomerDelegate.service.removeEventListener(FaultEvent.FAULT,fault);
		}

	}
}
package com.tqcm.bussiness
{
	import com.adobe.cairngorm.business.ServiceLocator;
	import com.tqcm.vo.Customer;
	
	import mx.rpc.IResponder;
	import mx.rpc.remoting.RemoteObject;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	public class AddCustomerDelegate
	{
		public var responder:IResponder
		public var service:RemoteObject;
		public function AddCustomerDelegate(responder:IResponder)
		{
			this.responder = responder;
			this.service = ServiceLocator.getInstance().getRemoteObject("customerDao");
		}
		
		public function execute(customer:Customer):void{
			service.addEventListener(ResultEvent.RESULT,responder.result);
			service.addEventListener(FaultEvent.FAULT,responder.fault);			
			service.addCustomer(customer);
		}
	}
}
<?xml version="1.0" encoding="utf-8"?>
<cairngorm:ServiceLocator xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:cairngorm="com.adobe.cairngorm.business.*">
	<mx:RemoteObject id="customerDao" showBusyCursor="false" destination="newsDao" endpoint="http://localhost:8080/tqcm/messagebroker/amf"/>	
</cairngorm:ServiceLocator>