Chinaunix首页 | 论坛 | 博客
  • 博客访问: 599888
  • 博文数量: 60
  • 博客积分: 3993
  • 博客等级: 中校
  • 技术积分: 1572
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-08 17:08
文章分类

全部博文(60)

文章存档

2012年(7)

2011年(35)

2010年(8)

2009年(7)

2008年(3)

分类: Python/Ruby

2011-06-02 10:00:10

描述一下自己对PureMVC的理解和使用,以下代码仅为说明自己使用的方式,不是完整的代码,如有问题请大家指教。
 
NameSpace.as : 定义常量
  1. package com.me.myapp
  2. {
  3.     
  4.     public class NameSpace
  5.     {
  6.         //Controller Command

  7.         public static const STARTUP                                :String = "startup";
  8.         
  9.         public static const CONN_SRV                            :String = "ConnSrv";
  10.         public static const CONN_SUCCESS                        :String    = "ConnSuccess";
  11.         public static const CONN_FAILED                            :String = "ConnFailed";

  12.         public static const SEND                                :String = "Send";        
  13.         public static const SEND_SUCCESS                        :String = "SendSuccess";
  14.         public static const SEND_FAILED                            :String = "SendFailed";    

  15.         public static const RECV                                :String = "Recv";        
  16.         public static const RECV_SUCCESS                        :String = "RecvSuccess";
  17.         public static const RECV_FAILED                            :String = "RecvFailed";    
  18.                     
  19.         public static const LOGIN                                :String = "Login";
  20.         public static const LOGIN_SUCCESS                        :String = "LoginSuccess";
  21.         public static const LOGIN_FAILED                        :String = "LoginFailed";
  22.         
  23.         public static const LOGOUT                                :String = "Logout";
  24.         public static const LOGOUT_SUCCESS                        :String = "LogoutSuccess";
  25.         
  26.                 
  27.         //Model Proxy

  28.         public static const VIEWSTACK_PROXY                        :String = 'ViewStackProxy';
  29.         public static const COMMAND_PROXY                        :String = 'CommandProxy';
  30.         public static const CONN_PROXY                            :String = 'ConnProxy';        

  31.         //View Mediator

  32.         public static const APPLICATION_MEDIATOR                :String = "ApplicationMediator";
  33.         public static const LOGIN_PANEL_MEDIATOR                :String = "LoginPanelMediator";
  34.         public static const LOGOUT_PANEL_MEDIATOR                :String = "LogoutPanelMediator";
  35.         public static const LOGOUT_BUTTON_MEDIATOR                :String = "LogoutButtonMediator";
  36.         public static const RECONN_BUTTON_MEDIATOR                :String = "ReconnButtonMediator";
  37.         
  38.         //View Status

  39.         public static const    LOGIN_PAGE_CREATED                    :String    = "BeforeLogin";
  40.         public static const    START_PAGE_CREATED                    :String    = "AfterLogin";
  41.         public static const    LOGOUT_PAGE_CREATED                     :String    = "AfterLogout";
  42.     
  43.         //ViewStack index

  44.         public static const PAGE_INDEX_LOGIN                    :Number = 0;
  45.         public static const PAGE_INDEX_START                    :Number = 1;
  46.         public static const PAGE_INDEX_LOGOUT                    :Number = 2;

  47.         //Init

  48.         
  49.         //Method

  50.         public static const    LOGIN_BUTTON_CLICK                    :String    = "LoginButtonClick";
  51.         public static const    LOGOUT_BUTTON_CLICK                    :String    = "LogoutButtonClick";
  52.         public static const RECONN_BUTTON_CLICK                    :String = "ReconnButtonClick";

  53.         //PopUpWindow


  54.         //remove PopUpWindow        


  55.     }
  56. }
 
HelloMVC.mxml : 初始化Application facade
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application
  3.     xmlns:mx=""
  4.     xmlns:app_comp="com.me.myapp.view.app.components.*"
  5.     layout="vertical"
  6.     creationComplete="init()"
  7.     xmlns:pages="com.me.myapp.view.*"
  8. >

  9.     <mx:Script>
  10.         <![CDATA[
  11.             import mx.collections.ArrayCollection;

  12.             import com.me.myapp.NameSpace;

  13.             //获取ApplicationFacade

  14.             import com.me.myapp.ApplicationFacade;
  15.             
  16.             import flash.system.Security;
  17.             import mx.controls.Alert;
  18.                             
  19.             //使用getInstance类方法初始化facade变量,Facade和相关的Module View Controller都会被实例化

  20.             private var facade:ApplicationFacade = ApplicationFacade.getInstance();
  21.             
  22.             public function init():void
  23.             {
  24.                 //启动整个PureMVC机制

  25.                 facade.startup(this); //this在一个类里面指代类'自己'

  26.                 //INFO: 除了顶层的Application,其他视图组件都不需要(不应该)和Facade交互

  27.                 
  28.                 //dispatchEvent LOGIN_PAGE_CREATED

  29.                 dispatchEvent(new Event(NameSpace.LOGIN_PAGE_CREATED,true));
  30.                 
  31.             }
  32.             
  33.             //使用viewStack跳转页面

  34.             [Bindable] public var currentViewSelector:Number = NameSpace.PAGE_INDEX_LOGIN;
  35.             [Bindable] public var activeView:Object;
  36.             

  37.         ]]>
  38.     </mx:Script>
  39.     <mx:Binding source="appView.selectedChild" destination="activeView"/>
  40.     <mx:ApplicationControlBar id="applicationControlBar" width="100%">
  41.         <mx:Image source="icon/logo.gif" width="126" height="45"/>
  42.         <app_comp:TopMenuBar id="topMenuBar" styleName="security(view_topMenuBar,enabled)"/>
  43.         <mx:Label text="用户:" fontSize="14"/>
  44.         <mx:Label id="userLabel" fontSize="14" color="#FAFAFA"/>
  45.         <mx:Label text="角色:" fontSize="14"/>
  46.         <app_comp:UserRoleComboBox id="userRoleComboBox"/>
  47.         <app_comp:LogoutButton id="logoutButton"/>
  48.         <mx:Spacer width="100%"/>
  49.         <mx:Label text="状态:" fontSize="14"/>
  50.         <mx:Label id="statusLabel" fontSize="14" color="#FAFAFA"/>
  51.     </mx:ApplicationControlBar>
  52.     <mx:ViewStack id="appView" resizeToContent="true" selectedIndex="{currentViewSelector}" width="100%" height="100%">
  53.         <pages:LoginPage id="loginPage" creationComplete="dispatchEvent(new Event(NameSpace.LOGIN_PAGE_CREATED,true))" width="100%" height="100%"/>
  54.         <pages:StartPage id="startPage" creationComplete="dispatchEvent(new Event(NameSpace.START_PAGE_CREATED,true))" width="100%" height="100%"/>
  55.         <pages:LogoutPage id="logoutPage" creationComplete="dispatchEvent(new Event(NameSpace.LOGOUT_PAGE_CREATED,true))" width="100%" height="100%"/>
  56.     </mx:ViewStack>
  57. </mx:Application>

 

ApplicationFacade.as : 启动PureMVC,注册Command,建立Notification与Command的映射

  1. package com.me.myapp
  2. {
  3.     //import NameSpace

  4.     import com.me.myapp.controller.*;
  5.     import com.me.myapp.model.*;
  6.     import com.me.myapp.view.*;
  7.     
  8.     import org.puremvc.as3.interfaces.IFacade;
  9.     import org.puremvc.as3.patterns.facade.Facade;
  10.     
  11.     //myapp的Facade类

  12.     public class ApplicationFacade extends Facade implements IFacade //继承自Facade类,Facade类实现了IFacade接口

  13.     {
  14.         //Facade类是整个系统其他角色相互访问的核心


  15.         public function ApplicationFacade()
  16.         {
  17.             super(); //使用super()显式地调用其直接超类(extends Facade)的构造函数

  18.         }

  19.         //得到ApplicationFacade的工厂方法

  20.         public static function getInstance():ApplicationFacade
  21.         {
  22.             if (instance == null)
  23.             {
  24.                 instance = new ApplicationFacade();
  25.             }
  26.             return instance as ApplicationFacade;
  27.         }
  28.         

  29.         //启动PureMVC,在应用程序中调用此方法,并传递应用程序本身的引用

  30.         public function startup(app:HelloMVC):void
  31.         {
  32.             sendNotification(NameSpace.STARTUP,app); //派发STARTUP的Notification,并将app作为参数

  33.         }

  34.         //注册Command,建立Notification与Command的映射

  35.         override protected function initializeController():void //覆盖initializeController方法

  36.         {
  37.             super.initializeController();
  38.             
  39.             //建立Notification与Command之间的关系

  40.             trace("建立Notification与Command之间的关系");
  41.             registerCommand(NameSpace.STARTUP,StartupCommand); //初始化Model和View
  42.             registerCommand(NameSpace.CONN_SRV,ConnCommand);
  43.             registerCommand(NameSpace.CONN_SUCCESS,ConnSuccessCommand);
  44.             registerCommand(NameSpace.CONN_FAILED,ConnFailedCommand);    
  45.             registerCommand(NameSpace.SEND,SendCommand);                    
  46.             registerCommand(NameSpace.RECV,RecvCommand);
  47.             registerCommand(NameSpace.RECV_SUCCESS,RecvSuccessCommand);
  48.             registerCommand(NameSpace.RECV_FAILED,RecvFailedCommand);
  49.             registerCommand(NameSpace.LOGIN,LoginCommand);
  50.             registerCommand(NameSpace.LOGIN_SUCCESS,LoginSuccessCommand);
  51.             registerCommand(NameSpace.LOGIN_FAILED,LoginFailedCommand);                    
  52.             registerCommand(NameSpace.LOGOUT,LogoutCommand);
  53.             registerCommand(NameSpace.LOGOUT_SUCCESS,LogoutSuccessCommand);
  54.                      
  55.         }        
  56.     }
  57. }

 

M

Conn.vo : 定义值对象(Data Object)

  1. package com.me.myapp.model.vo
  2. {
  3.     import flash.net.Socket;
  4.     import flash.utils.ByteArray;

  5.     public class ConnVO
  6.     {
  7.         public var socket:Socket = new Socket();

  8.         public var connIP:String = "192.168.11.145"; //test -> 192.168.10.145

  9.         public var connPort:uint = 6001;
  10.         public var connMask:uint = 8; //8:无校验,压缩,无加密

  11.         public var connStatus:int = 0; //0 no connected, 1 connected,<0 error

  12.         [Bindable] public var isDisconn:Boolean = true;
  13.         
  14.         public var sendBuf:String;
  15.         //STEP

  16.         //0:未连接 1:正在连接 2:已连接 3:发送消息 4:消息接受完成

  17.         // -2:连接失败 -3:发送消息失败 -4:消息接受失败

  18.         public var STEP:int = 0; //状态

  19.         
  20.         public var bufBytesArr:ByteArray = new ByteArray(); //接收缓存

  21.         public var state:int = 0; //包接收状态 -1:包头不完整 -2:包头完整但数据未接收完 0:初始状态 1:包头已接收,确认该包长度 2:只有一个数据包并且接收完整 3:有一个以上的包

  22.         
  23.         public var waitBytes:int = 0;
  24.         }
  25. }

ConnProxy.as :用于访问ConnVO的Proxy

  1. package com.me.myapp.model
  2. {
  3.     //import NameSpace

  4.     import com.me.myapp.NameSpace;
  5.     import com.me.myapp.model.vo.*;
  6.     
  7.     import org.puremvc.as3.interfaces.IProxy;
  8.     import org.puremvc.as3.patterns.proxy.Proxy;
  9.         
  10.     public class ConnProxy extends Proxy implements IProxy
  11.     {

  12.         public function ConnProxy(data:Object=null):void
  13.         {
  14.             super(NameSpace.CONN_PROXY,new ConnVO());

  15.         }

  16.         public function get connVO():ConnVO
  17.         {
  18.             return data as ConnVO;
  19.         }    
  20.     }
  21. }

 

V

LoginPanel.mxml : UI

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Panel xmlns:mx=""
  3.     layout="vertical" title="LoginPanel" creationComplete="{init()}">

  4.     <mx:Script>
  5.         <![CDATA[
  6.             import com.me.myapp.NameSpace;
  7.             import flash.events.Event;
  8.             import flash.events.EventDispatcher;
  9.             import flash.events.KeyboardEvent;
  10.             import mx.events.FlexEvent;
  11.             import mx.validators.Validator;

  12.             public function init():void
  13.             {
  14.                 trace("init LoginPanel");
  15.                 txi_userName.addEventListener(FlexEvent.ENTER,enter_click);
  16.                 txi_userPassword.addEventListener(FlexEvent.ENTER,enter_click);    
  17.             }    

  18.             public function enter_click(e:FlexEvent):void
  19.             {
  20.                 click();
  21.             }
  22.             
  23.             public function click():void
  24.             {
  25.                 trace("click login button");
  26.                 dispatchEvent(new Event(NameSpace.LOGIN_BUTTON_CLICK,true));
  27.                 
  28.             }
  29.             public function txi_userName_click():void
  30.             {
  31.                 txi_userName.text = '';
  32.             }

  33.         ]]>
  34.     </mx:Script>
  35.     <mx:StringValidator id="txi_userNameValidator"
  36.         source="{txi_userName}"
  37.         requiredFieldError="用户名不能为空"
  38.         property="text"
  39.         minLength="3" />
  40.     <mx:StringValidator id="txi_userPasswordValidator"
  41.         source="{txi_userPassword}"
  42.         requiredFieldError="密码不能为空"
  43.         property="text"
  44.         minLength="3" />
  45.                 
  46.     <mx:Text text="虚拟化管理平台" fontSize="24" color="#0F5AE2" fontWeight="bold" width="100%" textAlign="center"/>
  47.     <mx:Form width="100%">
  48.         <mx:FormItem label="用户名">
  49.             <mx:TextInput id="txi_userName" text="zhangminjie" click="{txi_userName_click()}"/>

  50.         </mx:FormItem>
  51.         <mx:FormItem label="密码">
  52.             <mx:TextInput id="txi_userPassword" displayAsPassword="true" text=""/>
  53.         </mx:FormItem>
  54.     </mx:Form>
  55.     <mx:Button width="100%" label="Login" id="bt_login" click="{click()}"/>
  56.     <mx:ControlBar id="ControlBar">
  57.         <mx:Label id="statusLabel"/>
  58.     </mx:ControlBar>
  59.     
  60. </mx:Panel>

LoginPanelMediator.as : UI相关的Mediator

  1. package com.me.myapp.view.login.mediators
  2. {
  3.     import com.me.myapp.NameSpace;
  4.     import com.me.myapp.model.UserProxy;
  5.     import com.me.myapp.view.login.components.*;
  6.     
  7.     import flash.events.Event;
  8.     
  9.     import mx.controls.Alert;
  10.     
  11.     import org.puremvc.as3.interfaces.IMediator;
  12.     import org.puremvc.as3.interfaces.INotification;
  13.     import org.puremvc.as3.patterns.mediator.Mediator;
  14.     
  15.     public class LoginPanelMediator extends Mediator implements IMediator
  16.     {
  17.         public function LoginPanelMediator(viewComponent:LoginPanel):void
  18.         {
  19.             super(NameSpace.LOGIN_PANEL_MEDIATOR,viewComponent);
  20.             trace("触发loginPanelMediator()");
  21.             //try conn

  22.             sendNotification(NameSpace.CONN_SRV);
  23.             
  24.             //wait login button click

  25.             trace("loginPanel添加事件监听: NameSpace.LOGIN_BUTTON_CLICK -> onLogin");
  26.             loginPanel.addEventListener(NameSpace.LOGIN_BUTTON_CLICK,onLogin);            
  27.         }

  28.         private function onLogin(evt:Event=null):void
  29.         {
  30.             trace("触发onLogin");
  31.             if (loginPanel.txi_userName.text != "" && loginPanel.txi_userPassword.text != "")
  32.             {
  33.                 //local reference to the UserProxy

  34.                 var _userProxy:UserProxy = facade.retrieveProxy(NameSpace.USER_PROXY) as UserProxy;
  35.                 _userProxy.userVO.UserName = loginPanel.txi_userName.text;
  36.                 _userProxy.userVO.UserPassword = loginPanel.txi_userPassword.text;
  37.                 sendNotification(NameSpace.LOGIN);
  38.             } else
  39.             {
  40.                 Alert.show("用户名或密码不能为空");            
  41.             }
  42.         }


  43.         public function get loginPanel():LoginPanel
  44.         {
  45.             return viewComponent as LoginPanel;
  46.         }    
  47.         
  48.         override public function listNotificationInterests():Array
  49.         {
  50.             return [    
  51.                         NameSpace.LOGIN_SUCCESS,
  52.                         NameSpace.GET_DATACENTERS_SUCCESS,
  53.                         NameSpace.GET_SERVERGROUPS_SUCCESS,
  54.                         NameSpace.GET_PHYSERVERGROUPMEMBER_SUCCESS,
  55.                         NameSpace.GET_PHYSERVERINFO_SUCCESS,
  56.                         NameSpace.GET_PHYSERVERINGROUP_SUCCESS,
  57.                         NameSpace.GET_ROLES_SUCCESS,
  58.                         NameSpace.GET_USERROLES_SUCCESS,                        
  59.                         NameSpace.GET_ALLUSER_ROLES_SUCCESS,
  60.                     ];                
  61.         }
  62.     
  63.         //handle

  64.         override public function handleNotification(notification:INotification):void
  65.         {    
  66.             switch(notification.getName())
  67.             {
  68.                 case    NameSpace.LOGIN_SUCCESS:
  69.                         trace("LoginPanelMediator: handleNotification -> LOGIN_SUCCESS");
  70.                         trace("尝试GET_DATACENTERS");
  71.                         sendNotification(NameSpace.GET_DATACENTERS);
  72.                         break;
  73.                 case    NameSpace.GET_DATACENTERS_SUCCESS:
  74.                         trace("LoginPanelMediator: handleNotification -> GET_DATACENTERS_SUCCESS");
  75.                         trace("尝试GET_SERVERGROUPS");
  76.                         sendNotification(NameSpace.GET_SERVERGROUPS);
  77.                         break;    
  78.                 case    NameSpace.GET_SERVERGROUPS_SUCCESS:
  79.                         trace("LoginPanelMediator: handleNotification -> GET_SERVERGROUPS_SUCCESS");
  80.                         trace("尝试GET_PHYSERVERGROUPMEMBER");
  81.                         sendNotification(NameSpace.GET_PHYSERVERGROUPMEMBER);
  82.                         break;    
  83.                 case    NameSpace.GET_PHYSERVERGROUPMEMBER_SUCCESS:
  84.                         trace("LoginPanelMediator: handleNotification -> GET_PHYSERVERGROUPMEMBER_SUCCESS");
  85.                         trace("尝试GET_PHYSERVERINFO");
  86.                         sendNotification(NameSpace.GET_PHYSERVERINFO);
  87.                         break;                                                                        
  88.                 case    NameSpace.GET_PHYSERVERINFO_SUCCESS:
  89.                         trace("LoginPanelMediator: handleNotification -> GET_PHYSERVERINFO_SUCCESS");
  90.                         trace("尝试GET_PHYSERVERINGROUP");
  91.                         sendNotification(NameSpace.GET_PHYSERVERINGROUP);
  92.                         break;                        
  93.                 case    NameSpace.GET_PHYSERVERINGROUP_SUCCESS:    
  94.                         trace("LoginPanelMediator: handleNotification -> GET_PHYSERVERINGROUP_SUCCESS");
  95.                         trace("尝试GET_ROLES");
  96.                         sendNotification(NameSpace.GET_ROLES);
  97.                         break;
  98.                 case    NameSpace.GET_ROLES_SUCCESS:
  99.                         trace("LoginPanelMediator: handleNotification -> GET_ROLES_SUCCESS");
  100.                         trace("尝试GET_USERROLES");
  101.                         sendNotification(NameSpace.GET_USERROLES);
  102.                         break;    
  103.                 case    NameSpace.GET_USERROLES_SUCCESS:
  104.                         trace("LoginPanelMediator: handleNotification -> GET_USERROLES_SUCCESS");
  105.                         trace("尝试GET_ALLUSER_ROLES");
  106.                         sendNotification(NameSpace.GET_ALLUSER_ROLES);    
  107.                         break;                                
  108.                 case    NameSpace.GET_ALLUSER_ROLES_SUCCESS:
  109.                         trace("LoginPanelMediator: handleNotification -> GET_ALLUSER_ROLES_SUCCESS");
  110.                         trace("尝试跳转到START_PAGE (START_PAGE_CREATED)");
  111.                         sendNotification(NameSpace.START_PAGE_CREATED);
  112.                         break;                    
  113.             }
  114.         
  115.         }
  116.         
  117.     }
  118. }

 

C

StartupCommand.as : 初始化Model和View

  1. package com.me.myapp.controller
  2. {
  3.     import org.puremvc.as3.interfaces.*;
  4.     import org.puremvc.as3.patterns.command.*;    
  5.     
  6.     //程序开始时执行的MacroCommand

  7.     public class StartupCommand extends MacroCommand
  8.     {
  9.         //添加子Command初始化MacroCommand

  10.         override protected function initializeMacroCommand():void
  11.         {
  12.             //添加2个子Command,执行时按照先进先出顺序

  13.             addSubCommand(ModelPrepCommand); //初始化Model

  14.             addSubCommand(ViewPrepCommand); //初始化View

  15.         }
  16.     }
  17. }

ModePrepCommand.as

  1. package com.me.myapp.controller
  2. {
  3.     import com.me.myapp.*;
  4.     import com.me.myapp.model.*;
  5.     
  6.     import org.puremvc.as3.interfaces.INotification;
  7.     import org.puremvc.as3.patterns.command.SimpleCommand;
  8.     
  9.     //创建Proxy对象,并注册

  10.     public class ModelPrepCommand extends SimpleCommand
  11.     {

  12.         public function ModelPrepCommand()
  13.         {
  14.         }
  15.         
  16.         //由MacroCommand调用

  17.         override public function execute(notification:INotification):void
  18.         {                        
  19.             facade.registerProxy(new ConnProxy());                
  20.         }
  21.         
  22.     }
  23. }

ViewPrepCommand.as

  1. package com.me.myapp.controller
  2. {
  3.     import com.me.myapp.*;
  4.     import com.me.myapp.view.ApplicationMediator;
  5.     
  6.     import org.puremvc.as3.interfaces.*;
  7.     import org.puremvc.as3.patterns.command.*;
  8.     import org.puremvc.as3.patterns.observer.*;
  9.     
  10.     //创建Mediator,并把他们注册到View

  11.     public class ViewPrepCommand extends SimpleCommand implements ICommand
  12.     
  13.     {
  14.         override public function execute(notification:INotification):void
  15.         {
  16.             //HelloMVC is PROJECT name

  17.             var app:HelloMVC = notification.getBody() as HelloMVC;
  18.             //创建并注册ApplicationMediator, ApplicationMediator用于操作Application View

  19.             facade.registerMediator(new ApplicationMediator(app));

  20.         }

  21.     }
  22. }

 

阅读(2204) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~