描述一下自己对PureMVC的理解和使用,以下代码仅为说明自己使用的方式,不是完整的代码,如有问题请大家指教。
NameSpace.as : 定义常量
- package com.me.myapp
- {
-
- public class NameSpace
- {
- //Controller Command
- public static const STARTUP :String = "startup";
-
- public static const CONN_SRV :String = "ConnSrv";
- public static const CONN_SUCCESS :String = "ConnSuccess";
- public static const CONN_FAILED :String = "ConnFailed";
- public static const SEND :String = "Send";
- public static const SEND_SUCCESS :String = "SendSuccess";
- public static const SEND_FAILED :String = "SendFailed";
- public static const RECV :String = "Recv";
- public static const RECV_SUCCESS :String = "RecvSuccess";
- public static const RECV_FAILED :String = "RecvFailed";
-
- public static const LOGIN :String = "Login";
- public static const LOGIN_SUCCESS :String = "LoginSuccess";
- public static const LOGIN_FAILED :String = "LoginFailed";
-
- public static const LOGOUT :String = "Logout";
- public static const LOGOUT_SUCCESS :String = "LogoutSuccess";
-
-
- //Model Proxy
- public static const VIEWSTACK_PROXY :String = 'ViewStackProxy';
- public static const COMMAND_PROXY :String = 'CommandProxy';
- public static const CONN_PROXY :String = 'ConnProxy';
- //View Mediator
- public static const APPLICATION_MEDIATOR :String = "ApplicationMediator";
- public static const LOGIN_PANEL_MEDIATOR :String = "LoginPanelMediator";
- public static const LOGOUT_PANEL_MEDIATOR :String = "LogoutPanelMediator";
- public static const LOGOUT_BUTTON_MEDIATOR :String = "LogoutButtonMediator";
- public static const RECONN_BUTTON_MEDIATOR :String = "ReconnButtonMediator";
-
- //View Status
- public static const LOGIN_PAGE_CREATED :String = "BeforeLogin";
- public static const START_PAGE_CREATED :String = "AfterLogin";
- public static const LOGOUT_PAGE_CREATED :String = "AfterLogout";
-
- //ViewStack index
- public static const PAGE_INDEX_LOGIN :Number = 0;
- public static const PAGE_INDEX_START :Number = 1;
- public static const PAGE_INDEX_LOGOUT :Number = 2;
- //Init
-
- //Method
- public static const LOGIN_BUTTON_CLICK :String = "LoginButtonClick";
- public static const LOGOUT_BUTTON_CLICK :String = "LogoutButtonClick";
- public static const RECONN_BUTTON_CLICK :String = "ReconnButtonClick";
- //PopUpWindow
- //remove PopUpWindow
- }
- }
HelloMVC.mxml : 初始化Application facade
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application
- xmlns:mx=""
- xmlns:app_comp="com.me.myapp.view.app.components.*"
- layout="vertical"
- creationComplete="init()"
- xmlns:pages="com.me.myapp.view.*"
- >
- <mx:Script>
- <![CDATA[
- import mx.collections.ArrayCollection;
- import com.me.myapp.NameSpace;
- //获取ApplicationFacade
- import com.me.myapp.ApplicationFacade;
-
- import flash.system.Security;
- import mx.controls.Alert;
-
- //使用getInstance类方法初始化facade变量,Facade和相关的Module View Controller都会被实例化
- private var facade:ApplicationFacade = ApplicationFacade.getInstance();
-
- public function init():void
- {
- //启动整个PureMVC机制
- facade.startup(this); //this在一个类里面指代类'自己'
- //INFO: 除了顶层的Application,其他视图组件都不需要(不应该)和Facade交互
-
- //dispatchEvent LOGIN_PAGE_CREATED
- dispatchEvent(new Event(NameSpace.LOGIN_PAGE_CREATED,true));
-
- }
-
- //使用viewStack跳转页面
- [Bindable] public var currentViewSelector:Number = NameSpace.PAGE_INDEX_LOGIN;
- [Bindable] public var activeView:Object;
-
- ]]>
- </mx:Script>
- <mx:Binding source="appView.selectedChild" destination="activeView"/>
- <mx:ApplicationControlBar id="applicationControlBar" width="100%">
- <mx:Image source="icon/logo.gif" width="126" height="45"/>
- <app_comp:TopMenuBar id="topMenuBar" styleName="security(view_topMenuBar,enabled)"/>
- <mx:Label text="用户:" fontSize="14"/>
- <mx:Label id="userLabel" fontSize="14" color="#FAFAFA"/>
- <mx:Label text="角色:" fontSize="14"/>
- <app_comp:UserRoleComboBox id="userRoleComboBox"/>
- <app_comp:LogoutButton id="logoutButton"/>
- <mx:Spacer width="100%"/>
- <mx:Label text="状态:" fontSize="14"/>
- <mx:Label id="statusLabel" fontSize="14" color="#FAFAFA"/>
- </mx:ApplicationControlBar>
- <mx:ViewStack id="appView" resizeToContent="true" selectedIndex="{currentViewSelector}" width="100%" height="100%">
- <pages:LoginPage id="loginPage" creationComplete="dispatchEvent(new Event(NameSpace.LOGIN_PAGE_CREATED,true))" width="100%" height="100%"/>
- <pages:StartPage id="startPage" creationComplete="dispatchEvent(new Event(NameSpace.START_PAGE_CREATED,true))" width="100%" height="100%"/>
- <pages:LogoutPage id="logoutPage" creationComplete="dispatchEvent(new Event(NameSpace.LOGOUT_PAGE_CREATED,true))" width="100%" height="100%"/>
- </mx:ViewStack>
- </mx:Application>
ApplicationFacade.as : 启动PureMVC,注册Command,建立Notification与Command的映射
- package com.me.myapp
- {
- //import NameSpace
- import com.me.myapp.controller.*;
- import com.me.myapp.model.*;
- import com.me.myapp.view.*;
-
- import org.puremvc.as3.interfaces.IFacade;
- import org.puremvc.as3.patterns.facade.Facade;
-
- //myapp的Facade类
- public class ApplicationFacade extends Facade implements IFacade //继承自Facade类,Facade类实现了IFacade接口
- {
- //Facade类是整个系统其他角色相互访问的核心
- public function ApplicationFacade()
- {
- super(); //使用super()显式地调用其直接超类(extends Facade)的构造函数
- }
- //得到ApplicationFacade的工厂方法
- public static function getInstance():ApplicationFacade
- {
- if (instance == null)
- {
- instance = new ApplicationFacade();
- }
- return instance as ApplicationFacade;
- }
-
- //启动PureMVC,在应用程序中调用此方法,并传递应用程序本身的引用
- public function startup(app:HelloMVC):void
- {
- sendNotification(NameSpace.STARTUP,app); //派发STARTUP的Notification,并将app作为参数
- }
- //注册Command,建立Notification与Command的映射
- override protected function initializeController():void //覆盖initializeController方法
- {
- super.initializeController();
-
- //建立Notification与Command之间的关系
- trace("建立Notification与Command之间的关系");
- registerCommand(NameSpace.STARTUP,StartupCommand); //初始化Model和View
- registerCommand(NameSpace.CONN_SRV,ConnCommand);
- registerCommand(NameSpace.CONN_SUCCESS,ConnSuccessCommand);
- registerCommand(NameSpace.CONN_FAILED,ConnFailedCommand);
- registerCommand(NameSpace.SEND,SendCommand);
- registerCommand(NameSpace.RECV,RecvCommand);
- registerCommand(NameSpace.RECV_SUCCESS,RecvSuccessCommand);
- registerCommand(NameSpace.RECV_FAILED,RecvFailedCommand);
- registerCommand(NameSpace.LOGIN,LoginCommand);
- registerCommand(NameSpace.LOGIN_SUCCESS,LoginSuccessCommand);
- registerCommand(NameSpace.LOGIN_FAILED,LoginFailedCommand);
- registerCommand(NameSpace.LOGOUT,LogoutCommand);
- registerCommand(NameSpace.LOGOUT_SUCCESS,LogoutSuccessCommand);
-
- }
- }
- }
M
Conn.vo : 定义值对象(Data Object)
- package com.me.myapp.model.vo
- {
- import flash.net.Socket;
- import flash.utils.ByteArray;
- public class ConnVO
- {
- public var socket:Socket = new Socket();
- public var connIP:String = "192.168.11.145"; //test -> 192.168.10.145
- public var connPort:uint = 6001;
- public var connMask:uint = 8; //8:无校验,压缩,无加密
- public var connStatus:int = 0; //0 no connected, 1 connected,<0 error
- [Bindable] public var isDisconn:Boolean = true;
-
- public var sendBuf:String;
- //STEP
- //0:未连接 1:正在连接 2:已连接 3:发送消息 4:消息接受完成
- // -2:连接失败 -3:发送消息失败 -4:消息接受失败
- public var STEP:int = 0; //状态
-
- public var bufBytesArr:ByteArray = new ByteArray(); //接收缓存
- public var state:int = 0; //包接收状态 -1:包头不完整 -2:包头完整但数据未接收完 0:初始状态 1:包头已接收,确认该包长度 2:只有一个数据包并且接收完整 3:有一个以上的包
-
- public var waitBytes:int = 0;
- }
- }
ConnProxy.as :用于访问ConnVO的Proxy
- package com.me.myapp.model
- {
- //import NameSpace
- import com.me.myapp.NameSpace;
- import com.me.myapp.model.vo.*;
-
- import org.puremvc.as3.interfaces.IProxy;
- import org.puremvc.as3.patterns.proxy.Proxy;
-
- public class ConnProxy extends Proxy implements IProxy
- {
- public function ConnProxy(data:Object=null):void
- {
- super(NameSpace.CONN_PROXY,new ConnVO());
- }
- public function get connVO():ConnVO
- {
- return data as ConnVO;
- }
- }
- }
V
LoginPanel.mxml : UI
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Panel xmlns:mx=""
- layout="vertical" title="LoginPanel" creationComplete="{init()}">
- <mx:Script>
- <![CDATA[
- import com.me.myapp.NameSpace;
- import flash.events.Event;
- import flash.events.EventDispatcher;
- import flash.events.KeyboardEvent;
- import mx.events.FlexEvent;
- import mx.validators.Validator;
- public function init():void
- {
- trace("init LoginPanel");
- txi_userName.addEventListener(FlexEvent.ENTER,enter_click);
- txi_userPassword.addEventListener(FlexEvent.ENTER,enter_click);
- }
- public function enter_click(e:FlexEvent):void
- {
- click();
- }
-
- public function click():void
- {
- trace("click login button");
- dispatchEvent(new Event(NameSpace.LOGIN_BUTTON_CLICK,true));
-
- }
- public function txi_userName_click():void
- {
- txi_userName.text = '';
- }
- ]]>
- </mx:Script>
- <mx:StringValidator id="txi_userNameValidator"
- source="{txi_userName}"
- requiredFieldError="用户名不能为空"
- property="text"
- minLength="3" />
- <mx:StringValidator id="txi_userPasswordValidator"
- source="{txi_userPassword}"
- requiredFieldError="密码不能为空"
- property="text"
- minLength="3" />
-
- <mx:Text text="虚拟化管理平台" fontSize="24" color="#0F5AE2" fontWeight="bold" width="100%" textAlign="center"/>
- <mx:Form width="100%">
- <mx:FormItem label="用户名">
- <mx:TextInput id="txi_userName" text="zhangminjie" click="{txi_userName_click()}"/>
- </mx:FormItem>
- <mx:FormItem label="密码">
- <mx:TextInput id="txi_userPassword" displayAsPassword="true" text=""/>
- </mx:FormItem>
- </mx:Form>
- <mx:Button width="100%" label="Login" id="bt_login" click="{click()}"/>
- <mx:ControlBar id="ControlBar">
- <mx:Label id="statusLabel"/>
- </mx:ControlBar>
-
- </mx:Panel>
LoginPanelMediator.as : UI相关的Mediator
- package com.me.myapp.view.login.mediators
- {
- import com.me.myapp.NameSpace;
- import com.me.myapp.model.UserProxy;
- import com.me.myapp.view.login.components.*;
-
- import flash.events.Event;
-
- import mx.controls.Alert;
-
- import org.puremvc.as3.interfaces.IMediator;
- import org.puremvc.as3.interfaces.INotification;
- import org.puremvc.as3.patterns.mediator.Mediator;
-
- public class LoginPanelMediator extends Mediator implements IMediator
- {
- public function LoginPanelMediator(viewComponent:LoginPanel):void
- {
- super(NameSpace.LOGIN_PANEL_MEDIATOR,viewComponent);
- trace("触发loginPanelMediator()");
- //try conn
- sendNotification(NameSpace.CONN_SRV);
-
- //wait login button click
- trace("loginPanel添加事件监听: NameSpace.LOGIN_BUTTON_CLICK -> onLogin");
- loginPanel.addEventListener(NameSpace.LOGIN_BUTTON_CLICK,onLogin);
- }
- private function onLogin(evt:Event=null):void
- {
- trace("触发onLogin");
- if (loginPanel.txi_userName.text != "" && loginPanel.txi_userPassword.text != "")
- {
- //local reference to the UserProxy
- var _userProxy:UserProxy = facade.retrieveProxy(NameSpace.USER_PROXY) as UserProxy;
- _userProxy.userVO.UserName = loginPanel.txi_userName.text;
- _userProxy.userVO.UserPassword = loginPanel.txi_userPassword.text;
- sendNotification(NameSpace.LOGIN);
- } else
- {
- Alert.show("用户名或密码不能为空");
- }
- }
- public function get loginPanel():LoginPanel
- {
- return viewComponent as LoginPanel;
- }
-
- override public function listNotificationInterests():Array
- {
- return [
- NameSpace.LOGIN_SUCCESS,
- NameSpace.GET_DATACENTERS_SUCCESS,
- NameSpace.GET_SERVERGROUPS_SUCCESS,
- NameSpace.GET_PHYSERVERGROUPMEMBER_SUCCESS,
- NameSpace.GET_PHYSERVERINFO_SUCCESS,
- NameSpace.GET_PHYSERVERINGROUP_SUCCESS,
- NameSpace.GET_ROLES_SUCCESS,
- NameSpace.GET_USERROLES_SUCCESS,
- NameSpace.GET_ALLUSER_ROLES_SUCCESS,
- ];
- }
-
- //handle
- override public function handleNotification(notification:INotification):void
- {
- switch(notification.getName())
- {
- case NameSpace.LOGIN_SUCCESS:
- trace("LoginPanelMediator: handleNotification -> LOGIN_SUCCESS");
- trace("尝试GET_DATACENTERS");
- sendNotification(NameSpace.GET_DATACENTERS);
- break;
- case NameSpace.GET_DATACENTERS_SUCCESS:
- trace("LoginPanelMediator: handleNotification -> GET_DATACENTERS_SUCCESS");
- trace("尝试GET_SERVERGROUPS");
- sendNotification(NameSpace.GET_SERVERGROUPS);
- break;
- case NameSpace.GET_SERVERGROUPS_SUCCESS:
- trace("LoginPanelMediator: handleNotification -> GET_SERVERGROUPS_SUCCESS");
- trace("尝试GET_PHYSERVERGROUPMEMBER");
- sendNotification(NameSpace.GET_PHYSERVERGROUPMEMBER);
- break;
- case NameSpace.GET_PHYSERVERGROUPMEMBER_SUCCESS:
- trace("LoginPanelMediator: handleNotification -> GET_PHYSERVERGROUPMEMBER_SUCCESS");
- trace("尝试GET_PHYSERVERINFO");
- sendNotification(NameSpace.GET_PHYSERVERINFO);
- break;
- case NameSpace.GET_PHYSERVERINFO_SUCCESS:
- trace("LoginPanelMediator: handleNotification -> GET_PHYSERVERINFO_SUCCESS");
- trace("尝试GET_PHYSERVERINGROUP");
- sendNotification(NameSpace.GET_PHYSERVERINGROUP);
- break;
- case NameSpace.GET_PHYSERVERINGROUP_SUCCESS:
- trace("LoginPanelMediator: handleNotification -> GET_PHYSERVERINGROUP_SUCCESS");
- trace("尝试GET_ROLES");
- sendNotification(NameSpace.GET_ROLES);
- break;
- case NameSpace.GET_ROLES_SUCCESS:
- trace("LoginPanelMediator: handleNotification -> GET_ROLES_SUCCESS");
- trace("尝试GET_USERROLES");
- sendNotification(NameSpace.GET_USERROLES);
- break;
- case NameSpace.GET_USERROLES_SUCCESS:
- trace("LoginPanelMediator: handleNotification -> GET_USERROLES_SUCCESS");
- trace("尝试GET_ALLUSER_ROLES");
- sendNotification(NameSpace.GET_ALLUSER_ROLES);
- break;
- case NameSpace.GET_ALLUSER_ROLES_SUCCESS:
- trace("LoginPanelMediator: handleNotification -> GET_ALLUSER_ROLES_SUCCESS");
- trace("尝试跳转到START_PAGE (START_PAGE_CREATED)");
- sendNotification(NameSpace.START_PAGE_CREATED);
- break;
- }
-
- }
-
- }
- }
C
StartupCommand.as : 初始化Model和View
- package com.me.myapp.controller
- {
- import org.puremvc.as3.interfaces.*;
- import org.puremvc.as3.patterns.command.*;
-
- //程序开始时执行的MacroCommand
- public class StartupCommand extends MacroCommand
- {
- //添加子Command初始化MacroCommand
- override protected function initializeMacroCommand():void
- {
- //添加2个子Command,执行时按照先进先出顺序
- addSubCommand(ModelPrepCommand); //初始化Model
- addSubCommand(ViewPrepCommand); //初始化View
- }
- }
- }
ModePrepCommand.as
- package com.me.myapp.controller
- {
- import com.me.myapp.*;
- import com.me.myapp.model.*;
-
- import org.puremvc.as3.interfaces.INotification;
- import org.puremvc.as3.patterns.command.SimpleCommand;
-
- //创建Proxy对象,并注册
- public class ModelPrepCommand extends SimpleCommand
- {
- public function ModelPrepCommand()
- {
- }
-
- //由MacroCommand调用
- override public function execute(notification:INotification):void
- {
- facade.registerProxy(new ConnProxy());
- }
-
- }
- }
ViewPrepCommand.as
- package com.me.myapp.controller
- {
- import com.me.myapp.*;
- import com.me.myapp.view.ApplicationMediator;
-
- import org.puremvc.as3.interfaces.*;
- import org.puremvc.as3.patterns.command.*;
- import org.puremvc.as3.patterns.observer.*;
-
- //创建Mediator,并把他们注册到View
- public class ViewPrepCommand extends SimpleCommand implements ICommand
-
- {
- override public function execute(notification:INotification):void
- {
- //HelloMVC is PROJECT name
- var app:HelloMVC = notification.getBody() as HelloMVC;
- //创建并注册ApplicationMediator, ApplicationMediator用于操作Application View
- facade.registerMediator(new ApplicationMediator(app));
- }
- }
- }
阅读(2204) | 评论(0) | 转发(0) |