Flex系列--6--整合Cairngorm2
?
Cairngorm 2? 概述
为什么不整合 Cairngorm 3 ?
Cairngorm 3 已经不是 Cairngorm 2 的升级。它由跨框架的“指导原则”、“工具”和“库”三部分组成,目的在于帮助开发者应用 Flex 和第三方框架。
开始整合
?
<?xml version="1.0" encoding="utf-8"?><cairngorm:ServiceLocator xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:cairngorm="http://www.adobe.com/2006/cairngorm"> <mx:RemoteObject id="employeeServiceRO" destination="employeeServiceDest" /></cairngorm:ServiceLocator>
package business{ import com.adobe.cairngorm.business.ServiceLocator; import mx.rpc.AsyncToken; import mx.rpc.IResponder; public class LoadEmployeesDelegate { private var responder:IResponder; private var service:Object; public function LoadEmployeesDelegate(responder:IResponder) { this.responder = responder; this.service = ServiceLocator.getInstance().getRemoteObject("employeeServiceRO"); } public function load():void { var token:AsyncToken = service.getList(); token.addResponder(responder); } }}
package model{ import com.adobe.cairngorm.model.IModelLocator; import com.adobe.cairngorm.CairngormMessageCodes; import com.adobe.cairngorm.CairngormError; import mx.collections.ArrayCollection; [Bindable] public class EmployeesModelLocator implements IModelLocator { public var employeesList:ArrayCollection; private static var _instance:EmployeesModelLocator; public function EmployeesModelLocator() { if (_instance != null ) { throw new CairngormError(CairngormMessageCodes.SINGLETON_EXCEPTION, "EmployeesModelLocator"); } _instance = this; } public static function getInstance():EmployeesModelLocator { if (_instance == null) { _instance = new EmployeesModelLocator(); } return _instance; } }}
package event{ import com.adobe.cairngorm.control.CairngormEvent; public class LoadEmployeesEvent extends CairngormEvent { static public var EVENT_ID:String = "loadEmployees"; public function LoadEmployeesEvent() { super(EVENT_ID); } }}
package command{ import com.adobe.cairngorm.commands.ICommand; import com.adobe.cairngorm.control.CairngormEvent; import mx.rpc.IResponder; import mx.controls.Alert; public class BaseCommand implements ICommand, IResponder { public function execute(event:CairngormEvent):void { } public function result(data:Object):void { } public function fault(info:Object):void { Alert.show("We are sorry, a system error has occurred. Please try again later."); } }}
package command{ import com.adobe.cairngorm.control.CairngormEvent; import business.LoadEmployeesDelegate; import model.EmployeesModelLocator; public class LoadEmployeesCommand extends BaseCommand { public override function execute(event:CairngormEvent):void { var delegate:LoadEmployeesDelegate = new LoadEmployeesDelegate(this); delegate.load(); } public override function result(data:Object):void { var employeesModelLocator:EmployeesModelLocator = EmployeesModelLocator.getInstance(); employeesModelLocator.employeesList = data.result; } }}
package controller{ import com.adobe.cairngorm.control.FrontController; import event.LoadEmployeesEvent; import command.LoadEmployeesCommand; public class FSController extends FrontController { public function FSController() { addCommand(LoadEmployeesEvent.EVENT_ID, LoadEmployeesCommand); } }}
?
<rds:Services xmlns:rds="business.*"/><router:FSController xmlns:router="controller.*"/>
?
import event.LoadEmployeesEvent; private function init():void { var loadEmployeesEvent:LoadEmployeesEvent = new LoadEmployeesEvent(); loadEmployeesEvent.dispatch(); }
?
import model.EmployeesModelLocator;
把以下内容
dataProvider="{employeeList}"
替换为
dataProvider="{EmployeesModelLocator.getInstance().employeesList}"