<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:control="CairngormDemo.business.control.*"> // 注意这个导入不能少 <mx:Script> <![CDATA[ import CairngormDemo.business.events.LoginEvent; import CairngormDemo.vo.UserVO; import CairngormDemo.model.LoginModelLocator; import mx.controls.Alert; import com.adobe.cairngorm.control.CairngormEventDispatcher; private function LoginHandle():void { if (this.username.text.length == 0 || this.password.text == null){ Alert.show("用户名不能为空!","错误"); } else if (this.password.text.length == 0 || this.password.text == null){ Alert.show("密码不能为空!","错误"); } else { var user:UserVO = new UserVO(); user.username = this.username.text; user.password = this.password.text; var ev:LoginEvent = new LoginEvent(user); // 触发事件(有一个参数) CairngormEventDispatcher.getInstance().dispatchEvent(ev); // 通过Cairngorm的事件跳转 } } ]]> </mx:Script> <control:LoginControl id="front"/> // 这个也不能少,跟最上面的那个导入的包一致 <mx:Panel width="350" height="250" layout="absolute" horizontalCenter="25" verticalCenter="18" fontSize="13"> <mx:Label x="39" y="25" text="用户名:"/> <mx:Label x="39" y="73" text="密 码:"/> <mx:TextInput x="119" y="23" id="username"/> <mx:TextInput x="119" y="71" id="password" displayAsPassword="true"/> <mx:Button x="39" y="121" label="登录" fontSize="12" click="LoginHandle()"/> </mx:Panel> </mx:Application>
package CairngormDemo.business.control{ import com.adobe.cairngorm.control.FrontController; import CairngormDemo.business.commands.LoginCommand; import CairngormDemo.business.events.LoginEvent; public class LoginControl extends FrontController { // 继承于ForntController public function LoginControl(){ super(); addCommand(LoginEvent.LOGIN_EVENT,LoginCommand); // 通过addCommand方法映射 } } }
package CairngormDemo.business.events { import CairngormDemo.vo.UserVO; import com.adobe.cairngorm.control.CairngormEvent; public class LoginEvent extends CairngormEvent { // 注意不是继承一般的Event事件 public static const LOGIN_EVENT:String="LoginEvent"; public var user:UserVO = null; public function LoginEvent(user:UserVO) { super(LOGIN_EVENT); this.user = user; // 保存参数 } } }
package CairngormDemo.business.commands { import CairngormDemo.business.delegates.UserDelegate; import CairngormDemo.business.events.LoginEvent; import com.adobe.cairngorm.commands.Command; import com.adobe.cairngorm.control.CairngormEvent; import mx.rpc.IResponder; public class LoginCommand implements Command, IResponder { public function execute(event:CairngormEvent):void{ // 通过这个方法接受事件 var userDelegate:UserDelegate = new UserDelegate(this); // 委托给UserDelegate 处理具体 var loginEvent:LoginEvent = event as LoginEvent; userDelegate.Login(loginEvent.user); // 调用委托中的方法,这里有一个参数 } // 返回的结果进行处理 public function result(data:Object):void{ var result:String=data.toString(); if(result=="OK"){ Alert.show("登录成功!"); } else { Alert.show("登录失败!"); } } // 处理过程出错时的处理 public function fault(info:Object):void{ Alert.show(info+""); } } }
package CairngormDemo.business.delegates { import mx.rpc.IResponder; import CairngormDemo.model.LoginModelLocator; import CairngormDemo.vo.UserVO; public class UserDelegate { private var responder:IResponder; public function UserDelegate(responder:IResponder) { this.responder = responder; } public function Login(user:UserVO):void { // 获取事件传递的参数,然后进行简单的验证 var result:Object; if (user.username=="lzc" && user.password=="123"){ result="OK"; } else { result="Fail" } responder.result(result); } } }
package CairngormDemo.vo { public class UserVO { public var username:String; public var password:String; } }
package CairngormDemo.model { import com.adobe.cairngorm.model.ModelLocator; import CairngormDemo.vo.UserVO; [Bindable] public class LoginModelLocator implements ModelLocator { private static var loginModelLocator:LoginModelLocator; // 私有 public static function getInstance():LoginModelLocator { // 单例模式 if (loginModelLocator == null) { loginModelLocator = new LoginModelLocator(); } return loginModelLocator; } public var NowUser:UserVO = new UserVO(); // 用于保存当前登录用户信息的变量 } }项目的结构: