flex puremvc demo

这个例子还是很久以前学习flex的时候看到的,由于工作比较忙,一直都没有时间来写博客,加上有长一段时间没有用Flex了,都感觉有些陌生了, 所以觉得吧学过的知识写到博客里,以后有时间自己来看看才不会忘记以前学过的东西。也可以便于知识共享。

puremvc是一个flex的MVC框架,以前也研究过Cairngrom,但是感觉Cairngrom的代码太沉长了,最后感觉还是puremvc要好一些。不过一般的小型项目也不需要这些框架了,一是代码会增加,二是学习这个需要一些时间和精力。

     这个例子对于初学puremvc的人我想会很有用。下面就来看看这个简单的Demo吧。
官方网站: http://puremvc.org/
    由于代码太多,我只贴一些重要的代码,完整Demo我会放在附件里。
ApplicationFacade.as
public class ApplicationFacade extends Facade implements IFacade
	{
		// Notification name constants
		public static const STARTUP:String 			= "startup";
		
		public static const NEW_USER:String 		= "newUser";
		public static const DELETE_USER:String 		= "deleteUser";
		public static const CANCEL_SELECTED:String	= "cancelSelected";
		
		public static const USER_SELECTED:String	= "userSelected";
		public static const USER_ADDED:String		= "userAdded";
		public static const USER_UPDATED:String		= "userUpdated";
		public static const USER_DELETED:String		= "userDeleted";

		public static const ADD_ROLE:String 		= "addRole";
		public static const ADD_ROLE_RESULT:String 	= "addRoleResult";
		
		
		/**
		 * Singleton ApplicationFacade Factory Method
		 */
		public static function getInstance() : ApplicationFacade {
			if ( instance == null ) instance = new ApplicationFacade( );
			return instance as ApplicationFacade;
		}
		
		/**
		 * Start the application
		 */
		 public function startup(app:Object):void
		 {
		 	sendNotification( STARTUP, app );	
		 }

		/**
		 * Register Commands with the Controller 
		 */
		override protected function initializeController( ) : void 
		{
			super.initializeController();			
			registerCommand( STARTUP, StartupCommand );
			registerCommand( DELETE_USER, DeleteUserCommand );
			registerCommand( ADD_ROLE_RESULT, AddRoleResultCommand );
		}
		
	}

pureMvc 的 facade 关系到 几个方法

首先 是注册  model  controller view  三个模块 是需要注册到 facade 才可以使用的
registerCommand
registerMediator
registerProxy

一下是控制层的一个类:
public class StartupCommand extends SimpleCommand implements ICommand
	{
		/**
		 * Register the Proxies and Mediators.
		 * 
		 * Get the View Components for the Mediators from the app,
		 * which passed a reference to itself on the notification.
		 */
		override public function execute( note:INotification ) : void	
		{
			//注册代理,以便于Mediator接受
			facade.registerProxy( new UserProxy() );
			facade.registerProxy( new RoleProxy() );
			
			var app:EmployeeAdmin = note.getBody() as EmployeeAdmin;
			facade.registerMediator( new UserFormMediator( app.userForm ) );
			//UserListMediator需要在初始化时显示数据,所以在构造方法初始化数据
			facade.registerMediator( new UserListMediator( app.userList ) );
			facade.registerMediator( new RolePanelMediator( app.rolePanel ) );
		}
	}

Mediator类:
public class UserListMediator extends Mediator implements IMediator
	{
		private var userProxy:UserProxy;

		public static const NAME:String = 'UserListMediator';

		public function UserListMediator( viewComponent:Object )
		{
			super( NAME, viewComponent );
			//添加事件监听器
			userList.addEventListener( UserList.NEW, 	onNew );
			userList.addEventListener( UserList.DELETE, onDelete);
			userList.addEventListener( UserList.SELECT, onSelect );
			//获得代理数据,程序初始化(StartUP)时调用
			userProxy = facade.retrieveProxy( UserProxy.NAME ) as UserProxy;
			userList.users = userProxy.users;
		}
		/**
		 * 获得视图组件
		 */
		private function get userList():UserList
		{
			return viewComponent as UserList;
		}
		
		private function onNew( event:Event ):void
		{
			var user:UserVO = new UserVO();
			sendNotification( ApplicationFacade.NEW_USER, user );
		}
		
		private function onDelete( event:Event ):void
		{
			sendNotification( ApplicationFacade.DELETE_USER,
							  userList.selectedUser );
		}
		
		private function onSelect( event:Event ):void
		{
			sendNotification( ApplicationFacade.USER_SELECTED,
							  userList.selectedUser );
		}
		/**
		 * 列出感兴趣的通知
		 */ 
		override public function listNotificationInterests():Array
		{
			return [
					ApplicationFacade.CANCEL_SELECTED,
					ApplicationFacade.USER_UPDATED
				   ];
		}
		
		override public function handleNotification( note:INotification ):void
		{
			switch ( note.getName() )
			{
				case ApplicationFacade.CANCEL_SELECTED:
					userList.deSelect();
					break;
					
				case ApplicationFacade.USER_UPDATED:
					userList.deSelect();
					break;
					
			}
		}
		
	}

    由于代码较多,我就不一一列举了,感兴趣的自己下载看吧。另外附带一个puremvc的很好资料,谢谢大家。

你可能感兴趣的:(框架,mvc,工作,Flex)