首先在项目中加入Cairngorm.swc文件
一、建立一个valueObject类
package com.dream.valueObjects
{
public class User
{
public var username:String;
public var password:String;
public var return_Check_Login:String;
public function User()
{
this.username = "";
this.password = "";
this.return_Check_Login = "";
}
}
}
二、ModelLocator 数据托管
package com.dream.model
{
import com.dream.valueObjects.User;
[Bindable]
public class ModelLocator
{
public var user:User = new User();
private static var __instance:ModelLocator = null;
public static function instance():ModelLocator{
if(null == __instance){
__instance = new ModelLocator();
}
return __instance;
}
}
}
三、创建Event事件
package com.dream.business.events.login
{
import com.adobe.cairngorm.control.CairngormEvent;
import com.dream.valueObjects.User;
public class CheckLoginUserEvent extends CairngormEvent
{
public static var EVENT_ID:String = "checkLoginUser";
public var user:User;
public function CheckLoginUserEvent(user:User)
{
super(EVENT_ID);
this.user = user;
}
}
}
四、信息接收
package com.dream.business.delegates
{
import com.adobe.cairngorm.business.ServiceLocator;
import com.dream.model.ModelLocator;
import com.dream.valueObjects.User;
import mx.rpc.AsyncToken;
import mx.rpc.IResponder;
import mx.rpc.remoting.RemoteObject;
public class CheckLoginUserDelegate
{
private var __locator:ServiceLocator = ServiceLocator.getInstance();
private var __remote:RemoteObject;
private var __responder:IResponder;
public function CheckLoginUserDelegate(responder:IResponder)
{
__remote = __locator.getRemoteObject("checkLogin");
__responder = responder;
}
public function returnCheckLoginUser(user:User):void{
var token:AsyncToken = __remote.checkUser(user.username,user.password);
token.addResponder(__responder);
}
}
}
五、信息处理
package com.dream.business.commands
{
import com.adobe.cairngorm.commands.ICommand;
import com.adobe.cairngorm.control.CairngormEvent;
import com.dream.business.delegates.CheckLoginUserDelegate;
import com.dream.business.events.login.CheckLoginUserEvent;
import com.dream.model.ModelLocator;
import com.dream.valueObjects.User;
import flash.net.URLRequest;
import flash.net.navigateToURL;
import mx.controls.Alert;
import mx.rpc.IResponder;
import mx.rpc.events.ResultEvent;
public class CheckLoginUserCommand implements ICommand,IResponder
{
public function execute(event:CairngormEvent):void{
var checkLoginUserEvent:CheckLoginUserEvent = event as CheckLoginUserEvent;
var checkLoginUserDelegate:CheckLoginUserDelegate = new CheckLoginUserDelegate(this);
checkLoginUserDelegate.returnCheckLoginUser(checkLoginUserEvent.user);
}
public function result(data:Object):void{
var resultEvent:ResultEvent = data as ResultEvent;
var user:User = new User();
user.return_Check_Login = resultEvent.result as String;
ModelLocator.instance().user = user;
if(user.return_Check_Login == "success"){
navigateToURL(new URLRequest("../dream/main.action"),"_self");
}else{
Alert.show(user.return_Check_Login);
}
}
public function fault(info:Object):void{
var resultEvent:ResultEvent = info as ResultEvent;
Alert.show("fault");
}
}
}
六、Event与Command关联
package com.dream.business
{
import com.adobe.cairngorm.control.FrontController;
import com.dream.business.commands.CheckLoginUserCommand;
import com.dream.business.events.login.CheckLoginUserEvent;
public class FSController extends FrontController
{
public function FSController(){
this.loadController();
}
private function loadController():void{
this.addCommand(CheckLoginUserEvent.EVENT_ID,CheckLoginUserCommand);
}
}
}
七、配置service
<?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="/dream/flex/messagebroker/amf"/>
<mx:ChannelSet id="channelSet" channels="{[myamf]}" />
<mx:RemoteObject id="checkLogin" destination="loginService" channelSet="{channelSet}" showBusyCursor="true"/>
-->
<mx:RemoteObject id="checkLogin" destination="loginService" showBusyCursor="true"/>
</rds:ServiceLocator>
八、在application中注册
<rds:Services xmlns:rds="com.dream.business.*"/>
<router:FSController xmlns:router="com.dream.business.*"/>