Hi.
很荣幸与您会话。我很欣赏Flex的表现,所以开始了学习(Flex)旅程。然而困难也理所当然的迎面而来——近来我使用Cairngorm,逐渐产生了
些疑问,想和各位讨论。盼望从此找到解决方案。
Cairngorm事件类与命令类的映射通常是一对一并只做一件事的,这会造成了as类膨胀,当然,为了避免此问题敝人采用了些小技巧(尽管不算是很合理
的)来解决:在Cairngorm事件中存在多个事件标识,在命令中判断 Cairngorm事件的标识从而决定做何种处理;这样的做法可以保持
Cairngorm事件类与命令类的一对一映射,而做到多种处理的情境。——对于这个问题,这是我当前的解决方法。
(只实现登录)
在View中分发
Cairngorm事件类
:
var userLoginEvent:UserEvent = new UserEvent(UserEvent.EVENT_ID_Login,this.loginUser);
userLoginEvent.dispatch();
前端控制器用户事件映射
:
addCommand(UserEvent.EVENT_ID_Login,UserCommand);
addCommand(UserEvent.EVENT_ID_DeleteBatch,UserCommand);
addCommand(UserEvent.EVENT_ID_Modify,UserCommand);
addCommand(UserEvent.EVENT_ID_QueryAll,UserCommand);
addCommand(UserEvent.EVENT_ID_Save,UserCommand);
Cairngorm的用户事件类:
package business.events
{
import com.adobe.cairngorm.control.CairngormEvent;
import mx.collections.ArrayCollection;
import vo.UserVO;
public class UserEvent extends CairngormEvent
{
public function UserEvent(type:String, user:UserVO=null, userList:ArrayCollection=null, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
this.user = user;
this.userList = userList;
}
public static const EVENT_ID_Save:String = "UserSaveEvent";
public static const EVENT_ID_DeleteBatch:String = "UserDeleteBatchEvent";
public static const EVENT_ID_Modify:String = "UserModifyEvent";
public static const EVENT_ID_QueryAll:String = "UserQueryAllEvent";
public static const EVENT_ID_Login:String = "UserLoginEvent";
private var _user:UserVO;
private var _userList:ArrayCollection;
public function get userList():ArrayCollection
{
return _userList;
}
public function set userList(value:ArrayCollection):void
{
_userList = value;
}
public function get user():UserVO
{
return _user;
}
public function set user(value:UserVO):void
{
_user = value;
}
}
}
用户命令类:
package business.commands
{
import business.delegates.UserDelegate;
import business.events.UserEvent;
import com.adobe.cairngorm.commands.ICommand;
import com.adobe.cairngorm.control.CairngormEvent;
import model.ModelLocator;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
import vo.UserVO;
public class UserCommand implements ICommand
{
public function UserCommand()
{
}
private static var _model:ModelLocator = ModelLocator.getInstence();
public function execute(event:CairngormEvent):void
{
var userList:ArrayCollection = (event as UserEvent).userList;
var user:UserVO = (event as UserEvent).user;
/**
* 判断事件
* */
switch(event.type){
case UserEvent.EVENT_ID_DeleteBatch:
deleteBatch(userList);
break;
case UserEvent.EVENT_ID_Modify:
modify(user);
break;
case UserEvent.EVENT_ID_QueryAll:
queryAll();
break;
case UserEvent.EVENT_ID_Save:
save(user);
break;
case UserEvent.EVENT_ID_Login:
login(user);
break;
default:
break;
}
}
/**
* 保存
* */
private function save(user:UserVO):void{
_model.isSuccess = false;
}
public static function save_Result(evt:ResultEvent):void{
_model.isSuccess = true;
}
/**
* 批量删除
* */
private function deleteBatch(userList:ArrayCollection):void{
_model.isSuccess = false;
}
public static function deleteBatch_Result(evt:ResultEvent):void{
_model.isSuccess = true;
}
/**
* 查询所有
* */
private function queryAll():void{
_model.isSuccess = false;
}
public static function queryAll_Result(evt:ResultEvent):void{
_model.isSuccess = true;
}
/**
* 修改
* */
private function modify(user:UserVO):void{
_model.isSuccess = false;
}
public static function modify_Result(evt:ResultEvent):void{
_model.isSuccess = true;
}
/**
* 修改
* */
private function login(user:UserVO):void{
_model.isSuccess = false;
var userDelegate:UserDelegate = new UserDelegate();
userDelegate.login(user);
}
public static function login_Result(evt:ResultEvent):void{
_model.isSuccess = true;
if(evt.result != null){
_model.user = evt.result as UserVO;
}
}
}
}
用户委托类:
package business.delegates
{
import com.adobe.cairngorm.business.ServiceLocator;
import mx.collections.ArrayCollection;
import mx.rpc.remoting.RemoteObject;
import mx.rpc.events.ResultEvent;
import com.adobe.cairngorm.business.ServiceLocator;
import business.commands.UserCommand;
import vo.UserVO;
public class UserDelegate
{
public function UserDelegate()
{
this.locator = ServiceLocator.getInstance();
}
private var locator:ServiceLocator ;
/**
* 登录
* */
public function login(user:UserVO):void{
//获取指定 远端服务
var service:RemoteObject = locator.getRemoteObject("roUserService");
//远端服务结果事件绑定
service.addEventListener(ResultEvent.RESULT, UserCommand.login_Result);
//执行远端服务
service.login(user);
}
}
}
远程服务对象:
<?xml version="1.0" encoding="utf-8"?>
<rds:ServiceLocator xmlns:rds="com.adobe.cairngorm.business.*"
xmlns:mx="http://www.adobe.com/2006/mxml">
<!-- 定义频道 -->
<mx:AMFChannel id="myamf" uri="spring/amf" />
<mx:ChannelSet id="cs" channels="{[myamf]}" />
<!-- 定义远程对象,destination与SpringBean对应,channelSet使用之前定义的频道 -->
<mx:RemoteObject id="roUserService" destination="userService" channelSet="{cs}" showBusyCursor="true" />
</rds:ServiceLocator>
问题:每当在View中分发了Cairngorm事件,进行远程交互,(一般情况下,只有Delegate或Command才能方便的得到结果)在
View层面如何得知远程服务是否已经响应?响应后结果又是什么呢?——这是敝人最迫切欲知的。希望各位同僚们参与讨论或提出解决的办法。感激不尽……