如何使用javascript的PureMVC框架 - Command/controller层
2012-07-02 14:42
561 查看
本实例的最终源文件下载地址
http://download.csdn.net/detail/laogong5i0/4400288
相关教程:
如何使用javascript的PureMVC框架 - Mediator/View层
继续上次 如何使用javascript的PureMVC框架 - 初始化 后我们在来看看command是如何使用的
Controller保存所有Command的映射。Command类是无状态的,只在需要时才被创建。
Command是负责处理复杂的数据逻辑的地方,它的使用归纳起来有两种
1. 通过发送Notification 来触发Command
2. addSubCommand() 来启动
口水不要太多,我们马上进入正题
在demo文件夹下建立文件夹controller ,然后在controller夹下建立一个叫StartupCommand.js的js文件,输入以下内容
跟着在demo文件夹下面建立一个文件夹abc来存放Noticefiction,在abc文件夹下建立NotificationNames.js文件,输入下面内容
然后在html文件里导入 StartupCommand.js和NotificationNames.js文件
完成后程序的目录树为下图
跟着回到上次如何使用javascript的PureMVC框架 - 初始化里面的 ApplicationFacade.js类
在 initializeController()方法里面添加代码
以及在startup()方法里面添加代码
程序在初始化的时候调用initializeController()方法,并使用registerCommand()方法注册了StartupCommand实例,
这样当收到由sendNotification()方法发送出来的NotificationNames.STARTUP消息后就会触发StartupCommand实例的initializeMacroCommand()方法了
运行后你会看到“收到由facade发来的”。
接下来我们来看看怎么使用addSubCommand() 来启动Command实例.
现在controller文件夹下新建PrepViewCommand.js文件,并输入以下内容
运行看是不是有输出?
最后文件夹目录树为
本实例的最终源文件下载地址
http://download.csdn.net/detail/laogong5i0/4400288
http://download.csdn.net/detail/laogong5i0/4400288
相关教程:
如何使用javascript的PureMVC框架 - Mediator/View层
继续上次 如何使用javascript的PureMVC框架 - 初始化 后我们在来看看command是如何使用的
Controller保存所有Command的映射。Command类是无状态的,只在需要时才被创建。
Command是负责处理复杂的数据逻辑的地方,它的使用归纳起来有两种
1. 通过发送Notification 来触发Command
2. addSubCommand() 来启动
口水不要太多,我们马上进入正题
在demo文件夹下建立文件夹controller ,然后在controller夹下建立一个叫StartupCommand.js的js文件,输入以下内容
var StartupCommand = Objs("sweeps.controller.StartupCommand", MacroCommand, { /** * @override * 添加Subcommands来启动PuerMVC组件 * 通常这里是先准备Model(即Proxy),然后是View(即Mediators); */ initializeMacroCommand: function( note ) { alert('收到由facade发来的'); //this.addSubCommand( PrepModelCommand ); //this.addSubCommand( PrepViewCommand ); } });
跟着在demo文件夹下面建立一个文件夹abc来存放Noticefiction,在abc文件夹下建立NotificationNames.js文件,输入下面内容
var NotificationNames = Objs("demo.abc.NotificationNames",{}); NotificationNames.STARTUP = "startup";
然后在html文件里导入 StartupCommand.js和NotificationNames.js文件
<script type="text/javascript" src="src/demo/abc/NotificationNames.js"></script> <script type="text/javascript" src="src/demo/controller/StartupCommand.js"></script>
完成后程序的目录树为下图
跟着回到上次如何使用javascript的PureMVC框架 - 初始化里面的 ApplicationFacade.js类
在 initializeController()方法里面添加代码
ApplicationFacade.$super.initializeController.call( this ); //注册StartupCommand,并让他监听STARTUP消息 this.registerCommand( NotificationNames.STARTUP, StartupCommand );
以及在startup()方法里面添加代码
//发送STARTUP消息给StartupCommand this.sendNotification( NotificationNames.STARTUP, app );这里主要是通过使用sendNotification(notice, app);方法发送Notification来触发StartupCommand 类。
程序在初始化的时候调用initializeController()方法,并使用registerCommand()方法注册了StartupCommand实例,
这样当收到由sendNotification()方法发送出来的NotificationNames.STARTUP消息后就会触发StartupCommand实例的initializeMacroCommand()方法了
运行后你会看到“收到由facade发来的”。
接下来我们来看看怎么使用addSubCommand() 来启动Command实例.
现在controller文件夹下新建PrepViewCommand.js文件,并输入以下内容
var PrepViewCommand = Objs("demo.controller.PrepViewCommand", SimpleCommand, { /** * @override */ execute: function( note ) { alert('使用addSubCommand方法调用Command实例'); } });然后在html文件里导入 PrepViewCommand.js文件。
<script type="text/javascript" src="src/demo/controller/PrepViewCommand.js"></script>接着在StartupCommand.js文件的initializeMacroCommand方法添加
this.addSubCommand( PrepViewCommand );这样就可以调用到PrepViewCommand.js文件的execut()方法了!
运行看是不是有输出?
最后文件夹目录树为
本实例的最终源文件下载地址
http://download.csdn.net/detail/laogong5i0/4400288
相关文章推荐
- 如何使用javascript的PureMVC框架 - 初始化
- 如何使用javascript的PureMVC框架 - Mediator/View层
- Unity开发小型游戏中如何便捷使用PureMVC框架
- Unity开发小型游戏中如何便捷使用PureMVC框架
- Unity开发小型游戏中如何便捷使用PureMVC框架
- Javascript 如何使用2维数组
- javascript代码如何实现打印框架里面的某个网页
- ABP框架中导航菜单的使用及JavaScript API获取菜单的方法
- Android-框架使用之-懒惰开发-如何提升开发效率
- 用户控件中使用了Javascript ,把2个控件放在页面上,那么页面上会有2个同名的javascript函数,如何处理
- 使用QUnit 框架测试Javascript代码
- 论如何使用scrapy框架登陆知乎
- 如何使用javascript做一个网页轮播图
- 如何在不用JavaScript的情况下使用CSS来实现图表功能
- flex框架pureMVC的使用:第一步
- 如何使用javascript向服务器提交数据(post)
- 如何在JavaScript中使用自定义事件
- 08如何使用express框架快速写项目_使用express完成项目的搭建
- JAVA_WEB项目之三大框架中不使用HibernateTemplate而使用SessionFactory以及如何使用注解
- .javascript如何使用继承