这个例子还是很久以前学习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的很好资料,谢谢大家。
分享到:
相关推荐
一个以pureMVC为flex前端框架,blazeds为通信服务器,spring和hibernate为服务端框架,实现了增删改等功能的完整实例,数据库使用mysql,部分关键地方加了注释,enjoy!
开发环境flash builder 4,sdk:flex4.1,PureMVC 3.2.04,简单的demo,结构清晰,有必要的注释,工程导入flash builder即可。用户名/密码为:admin/admin视为验证通过。
这是一个使用C#开发语言的基于PureMVC框架的Web登录Demo,是网上绝无仅有的实例。 本文以初学者的视角,详细地介绍PureMVC在ASP.NET的应用。 PureMVC不仅仅是Flex的MVC框架哦,而且它非常的小巧,还等什么呢,快来...
用pureMvc框架下的flexdemo
一个PureMVC框架下的Flex小实例。
演示:流形漫游器(Flex) 此演示说明了PureMVC框架的用法,该示例使用Flex应用程序中的组件,该组件漫游在用户导航时根据需要提取的XML文件中定义的节点网络。截屏地位生产平台/技术执照PureMVC AS3 / Flex演示– ...
该演示演示了WebORB服务与基于PureMVC的Flex客户端的协作,以执行登录操作。 截屏 地位 生产- 平台/技术 执照 PureMVC AS3演示-Flex / WebORB登录-版权所有:copyright:2008 Jens Krause PureMVC-版权所有:...
演示:应用程序骨架(Flex) 该演示演示了基于PureMVC的Flex应用程序的启动过程,该过程显示带有进度条的初始屏幕,直到加载了多个资源为止,之后向用户展示了实际的UI。 包括标准版和MultiCore版。地位生产-平台/...
演示:顺序(纯AS3) 此演示演示了如何使用AsyncCommand实用程序执行一系列命令,其中一些命令... 未经明确的事先书面许可,不得使用Futurescale,Inc.,PureMVC.org的名称或其贡献者的名称来认可或促销从该软件衍生
puremvc,极好的flex开源框架,这是一个demo
Parsley的一个Demo 比pureMVC更适合用在Flex 也可以在java blazeDS 中运行 http://coenraets.org/blog/2009/07/building-a-flex-application-with-the-parsley-framework/