Src目录下:
LoginForm.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml" fontSize="12" layout="vertical" horizontalAlign="center" title="用户登录" width="314">
<mx:Script>
<![CDATA[
import system.NameSpace;
]]>
</mx:Script>
<mx:Form width="100%" height="100%">
<mx:FormHeading label="敬请登录"/>
<mx:FormItem label="登录名称">
<mx:TextInput id="loginName"/>
</mx:FormItem>
<mx:FormItem label="登录密码">
<mx:TextInput id="loginPassword"/>
</mx:FormItem>
<mx:FormItem>
<mx:Button label="确定" id="loginBtn" click="dispatchEvent(new Event(NameSpace.LoginEvent))"/>
</mx:FormItem>
</mx:Form>
<mx:Label id="labStatus"/>
</mx:Panel>
PureLoginMVC.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application fontSize="12" xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()" layout="absolute">
<mx:Script>
<![CDATA[
import system.MyFacade;
import org.puremvc.as3.patterns.facade.Facade;
public function init():void{
var myFacade:MyFacade = MyFacade.getInstance();
myFacade.startup(this);
}
]]>
</mx:Script>
</mx:Application>
src目录下system包下:
MyFacade.as
package system
{
import Controller.ApplicationCommand;
import Controller.LoginCommand;
import org.puremvc.as3.interfaces.IFacade;
import org.puremvc.as3.patterns.facade.Facade;
public class MyFacade extends Facade implements IFacade
{
public function MyFacade()
{
super();
}
//实现MyFacade的单例
public static function getInstance():MyFacade{
if(instance == null){
instance = new MyFacade();
}
return instance as MyFacade;
}
//初始化
override protected function initializeController():void{
super.initializeController();
registerCommand(NameSpace.FacadeStart,ApplicationCommand);//注册开始命令
registerCommand(NameSpace.ApplicationLogin,LoginCommand);//注册登录命令
}
//开始的通知
public function startup(app:PureLoginMVC):void{
sendNotification(NameSpace.FacadeStart,app);
}
}
}
NameSpace.as
package system
{
public class NameSpace
{
public static const LoginProxyName:String = "LoginProxy";
public static const LoginSuccess:String = "loginSuccess";
public static const LoginFailed:String = "loginFailed";
public static const LoginEvent:String = "loginEvent";
public static const LoginMediatorName:String = "loginMediator";
public static const ApplicationLogin:String = "login";
public static const ApplicationMediatorName:String = "appliactionMediator";
public static const FacadeStart:String = "start";
}
}
src下Controller包
ApplicationCommand.as
package Controller
{
import org.puremvc.as3.interfaces.ICommand;
import org.puremvc.as3.patterns.command.MacroCommand;
public class ApplicationCommand extends MacroCommand implements ICommand
{
//构造器,可写可不写
public function ApplicationCommand()
{
super();
}
//写法一
override protected function initializeMacroCommand():void{
addSubCommand(ModelCommand);//添加model命令
addSubCommand(ViewCommand); //添加view命令
}
}
}
ModelCommand.as
package Controller
{
import model.LoginProxy;
import org.puremvc.as3.interfaces.ICommand;
import org.puremvc.as3.interfaces.INotification;
import org.puremvc.as3.patterns.command.SimpleCommand;
public class ModelCommand extends SimpleCommand implements ICommand
{
public function ModelCommand()
{
super();
}
override public function execute(notification:INotification):void
{
facade.registerProxy(new LoginProxy());//注册登录
}
}
}
ViewCommand。as
package Controller
{
import org.puremvc.as3.interfaces.ICommand;
import org.puremvc.as3.interfaces.INotification;
import org.puremvc.as3.patterns.command.SimpleCommand;
import view.ApplicationMediator;
import view.LoginMediator;
public class ViewCommand extends SimpleCommand implements ICommand
{
public function ViewCommand()
{
super();
}
//注册Mediator
override public function execute(notification:INotification):void
{
var app:PureLoginMVC = notification.getBody() as PureLoginMVC;
facade.registerMediator(new ApplicationMediator(app));//注册application中间人
facade.registerMediator(new LoginMediator(app));//注册登录中间人
}
}
}
LoginCommand。as
package Controller
{
import model.LoginProxy;
import model.vo.LoginVO;
import org.puremvc.as3.interfaces.ICommand;
import org.puremvc.as3.interfaces.INotification;
import org.puremvc.as3.patterns.command.SimpleCommand;
import system.NameSpace;
public class LoginCommand extends SimpleCommand implements ICommand
{
public function LoginCommand()
{
super();
}
override public function execute(notification:INotification):void{
var loginVO:LoginVO = notification.getBody() as LoginVO;
//var loginProxy:LoginProxy = new LoginProxy();
//从facade中获取proxy
var loginProxy:LoginProxy = facade.retrieveProxy(NameSpace.LoginProxyName) as LoginProxy;
loginProxy.CheckLogin(loginVO);
}
}
}
src下的model:
LoginProxy.as
package model
{
import model.vo.LoginVO;
import org.puremvc.as3.interfaces.IProxy;
import org.puremvc.as3.patterns.proxy.Proxy;
import system.NameSpace;
public class LoginProxy extends Proxy implements IProxy
{
public function LoginProxy()
{
super(NameSpace.LoginProxyName);//给登录通知一个参数名
}
//简单的进行判断
public function CheckLogin(vo:LoginVO):void{
if(vo.username =="test"&&vo.password == "test"){
sendNotification(NameSpace.LoginSuccess,vo);//发送成功的通知
}else{
sendNotification(NameSpace.LoginFailed);//发送失败的通知
}
}
}
}
model。vo下的:
LoginVO.as
package model.vo
{
public class LoginVO
{
public var username:String;
public var password:String;
}
}
src下的viewbao下:
ApplicationMediator。as
package view
{
import model.vo.LoginVO;
import mx.controls.Label;
import org.puremvc.as3.interfaces.IMediator;
import org.puremvc.as3.interfaces.INotification;
import org.puremvc.as3.patterns.mediator.Mediator;
import system.NameSpace;
public class ApplicationMediator extends Mediator implements IMediator
{
private var loginApplication:PureLoginMVC;
//构造器,获取application实例
public function ApplicationMediator(app:Object)
{
super(NameSpace.ApplicationMediatorName,app);
loginApplication = app as PureLoginMVC;
}
//监听通知
override public function listNotificationInterests():Array{
return[NameSpace.LoginSuccess];
}
//响应通知
override public function handleNotification(notification:INotification):void{
switch (notification.getName()){
case NameSpace.LoginSuccess:
setLoginSuc(notification.getBody() as LoginVO);
break;
default:
break;
}
}
private function setLoginSuc(loginVO:LoginVO):void{
var label:Label = new Label();
label.text = "欢迎光临!"+loginVO.username;
loginApplication.addChild(label);
}
}
}
LoginMediator.as
package view
{
import flash.events.Event;
import model.vo.LoginVO;
import mx.managers.PopUpManager;
import org.puremvc.as3.interfaces.IMediator;
import org.puremvc.as3.interfaces.INotification;
import org.puremvc.as3.patterns.mediator.Mediator;
import system.NameSpace;
public class LoginMediator extends Mediator implements IMediator
{
//获取应用实例
private var loginPanel:LoginForm;
private var loginApplication:PureLoginMVC;
public function LoginMediator(app:Object)
{
super(NameSpace.LoginMediatorName,app);
loginApplication = app as PureLoginMVC;
loginPanel = new LoginForm();
loginPanel.addEventListener(NameSpace.LoginEvent,onLogin);
PopUpManager.addPopUp(loginPanel,loginApplication);
PopUpManager.centerPopUp(loginPanel);
}
//监听通知
override public function listNotificationInterests():Array{
var arr:Array = new Array();
arr.push(NameSpace.LoginSuccess);
arr.push(NameSpace.LoginFailed);
return arr;
}
//响应通知
override public function handleNotification(notification:INotification):void{
switch(notification.getName()){
case NameSpace.LoginSuccess:
setLoginSuc();
break;
case NameSpace.LoginFailed:
setLoginFailed();
break;
default:
break;
}
}
//登录成功,移除登录窗口
private function setLoginSuc():void{
PopUpManager.removePopUp(loginPanel);
}
//登录失败,提示
private function setLoginFailed():void{
loginPanel.labStatus.text ="用户名密码有误,请重试";
loginPanel.loginName.setFocus();
}
private function onLogin(evt:Event):void{
if(loginPanel.loginName.text!=""&&loginPanel.loginPassword.text!=""){
var loginVO:LoginVO = new LoginVO();
loginVO.username = loginPanel.loginName.text;
loginVO.password = loginPanel.loginPassword.text;
sendNotification(NameSpace.ApplicationLogin,loginVO);
}else{
loginPanel.labStatus.text = "请输入用户名和密码";
}
}
}
}