三、登陆功能的实现
3.1 用户实体类User
定义用户名和密码。一般要加[Bindable]标签,标记为可绑定。
package vo { import com.adobe.cairngorm.vo.IValueObject; [Bindable] public class User implements IValueObject { public var userName:String = ""; public var password:String = ""; public function User() { } } }
3.2 remoteObject服务
基于cairngorm框架里的ServiceLocator标签,它是使用单例模式,在整个项目中只有一个实例,当在此标签内部定义RemoteObject等于后台通信服务的标签时,便会被存储起来再使用时,通过getRemoteObject方法获取。
<?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="generalRemoteObject" destination="fluorine" endpoint="http://192.1.1.113/liWebService/Gateway.aspx" source="Li.FluorineFx.Sample" showBusyCursor="true"/> </cairngorm:ServiceLocator>
3.3 后台代理类GeneralDelegate
首先获取到定义好的RemoteObject对象的引用,然后进行后台方法的调用。使用AsyncToken的addResponder来进行返回结果的处理。
/** * 常用的代理 */ package business { import com.adobe.cairngorm.business.ServiceLocator; import mx.rpc.AsyncToken; import mx.rpc.IResponder; import vo.User; public class GeneralDelegate { /** 应答器 */ private var responder:IResponder; private var service:Object; public function GeneralDelegate( responder:IResponder ) { this.responder = responder; this.service = ServiceLocator.getInstance().getRemoteObject("generalRemoteObject"); } /** * 登录 */ public function logon( user:User ):void { var call:AsyncToken = service.Logon(user.userName, user.password); call.addResponder(responder); } } }
3.4 Model类
使用单例模式,保存全局数据。
package models { import com.adobe.cairngorm.model.IModelLocator; [Bindable] public class GeneralModel implements IModelLocator { public var logonResult:String = ""; /** 单一引用对象 */ private static var generalModel:GeneralModel = null; [Deprecated(replacement="GeneralModel.getInstance()")] public function GeneralModel() { } public static function getInstance():GeneralModel { if( generalModel == null ) { generalModel = new GeneralModel(); } return generalModel; } } }
3.5 登录事件类LogonEvent
自定义事件,继承CairngormEvent,一般定义一些变量用来存储数据。
/** * 登陆操作事件 */ package control.event { import com.adobe.cairngorm.control.CairngormEvent; import vo.User; public class LogonEvent extends CairngormEvent { /** 操作登陆的用户 */ public var user:User; public static const LOGON:String = "logon"; public function LogonEvent(type:String, user:User) { this.user = user; super(type); } } }
3.6 事件注册类GeneralControl
用于对事件注册监听器。
package control { import com.adobe.cairngorm.control.FrontController; import control.event.*; import commands.*; public class GeneralControl extends FrontController { public function GeneralControl() { super.addCommand(LogonEvent.LOGON, LogonCommand );//注册,用户登陆命令 } } }
3.7 对应Command类
事件触发后的处理方法,和调用后台后返回结果的处理,例如更新model中的数据。
/** * 用户登陆,操作命令 */ package commands { import com.adobe.cairngorm.commands.ICommand; import com.adobe.cairngorm.control.CairngormEvent; import mx.rpc.IResponder; import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; import business.GeneralDelegate; import errors.ManageErrors; import models.GeneralModel; import control.event.LogonEvent; import vo.User; public class LogonCommand implements ICommand, IResponder { public function LogonCommand() { } /**------------------------------- Command接口方法 ----------------------------------------------*/ /** * 用户登陆,事件处理方法 */ public function execute( event:CairngormEvent ):void { var generalDelegate:GeneralDelegate = new GeneralDelegate( this ); generalDelegate.logon( (event as LogonEvent).user ); } /**------------------------------- IResponder接口方法 ---------------------------------------------*/ public function result( info:Object ):void { var result:Boolean = (info as ResultEvent).result as Boolean; if( result ) { GeneralModel.getInstance().logonResult = "登陆成功"; } else { GeneralModel.getInstance().logonResult = "登陆失败"; } } public function fault( info:Object ):void { ManageErrors.manageFaults( info as FaultEvent ); } } }
3.8 界面Application
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="1003" minHeight="600" fontSize="12" xmlns:control="control.*" xmlns:services="services.*"> <mx:Script> <![CDATA[ import control.event.LogonEvent; import models.GeneralModel; import vo.User; /** * 登陆按钮,事件处理 */ private function buttonLogon_clickHandler(event:MouseEvent):void { var user:User = new User(); user.userName = this.textInputUserName.text; user.password = this.textInputPassword.text; var logonEvent:LogonEvent = new LogonEvent(LogonEvent.LOGON, user); logonEvent.dispatch(); } ]]> </mx:Script> <control:GeneralControl /> <services:GeneralServices /> <mx:Binding source="GeneralModel.getInstance().logonResult" destination="textArea.text"/> <mx:VBox width="400" horizontalCenter="0" verticalCenter="90" horizontalAlign="center"> <mx:HBox verticalAlign="middle" borderStyle="solid"> <mx:Form > <mx:FormItem label="用户名:"> <mx:TextInput id="textInputUserName" width="150" /> </mx:FormItem> <mx:FormItem label="密码:"> <mx:TextInput id="textInputPassword" width="150" displayAsPassword="true" /> </mx:FormItem> </mx:Form> <mx:Button label="登陆" click="buttonLogon_clickHandler(event)" /> </mx:HBox> <mx:TextArea id="textArea" text="未登录" /> </mx:VBox> </mx:Application>