puremvc框架之Command

简介: 在前一篇 puremvc框架之hello world! 里,已经对这个框架有了一个大概的认识,不过在消息的处理上,有一个不太适合的地方: 为了完成响应消息,TextMediator亲自去监听自己感兴趣的消息类型,然后亲自来处理。

在前一篇 puremvc框架之hello world! 里,已经对这个框架有了一个大概的认识,不过在消息的处理上,有一个不太适合的地方:

为了完成响应消息,TextMediator亲自去监听自己感兴趣的消息类型,然后亲自来处理。要知道:Mediator属于View层(即:MVC中的V),它最好是什么也不干,仅仅与界面保持联系即可,对于如何响应消息这类粗活,应该交由Controller层(即MVC中的C)来处理最适合不过,所以这一章介绍如何把消息处理由V(View)转移到C(Controller)上来。

对于每类消息,我们可以创建一个与之对应的Command,以上回的CHANGE_TEXT消息类型来说,我们可以创建一个ChangeTextCommand,代码如下:

package mvc.controller
{
	import mvc.view.TextMediator;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.command.SimpleCommand;
	
	public class ChangeTextCommand extends SimpleCommand
	{
		public function ChangeTextCommand()
		{
			super();
		}
		
		public override function execute(notification:INotification):void{
			
			(facade.retrieveMediator(TextMediator.NAME) as TextMediator).txtInstance.text = notification.getBody() as String;
		}
	}
}

关键在于(facade.retrieveMediator(TextMediator.NAME) as TextMediator).txtInstance.text = notification.getBody() as String;这一句,它就是用来处理消息的,同时我们也注意到txtInstance原来为private属性,为了能让外部访问,必须改成public方法,修改后的TextMediator.as如下:

package mvc.view
{
	import spark.components.TextInput;
	import mvc.AppFacade;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	public class TextMediator extends Mediator implements IMediator
	{
		public static const NAME:String = "TextMediator";
		
		public function TextMediator(viewComponent:TextInput)
		{
			super(NAME,viewComponent);
		}
		
		/*
		public override function listNotificationInterests():Array{
			return [AppFacade.CHANGE_TEXT];
		}
		
		//响应消息
		public override function handleNotification(notification:INotification):void
		{
			switch(notification.getName()){
				case AppFacade.CHANGE_TEXT:
					this.txtInstance.text = notification.getBody() as String;
					break;
			}
		}
		*/
		
		//获取与之关联的UI界面上的“文本输入框”
		public function get txtInstance():TextInput{
			return viewComponent as TextInput;
		}
	}
}

有了ChangeTextCommand处理CHANGE_TEXT消息后,TextMediator中就不需要亲自来监听并处理该消息了,所以把这部分去掉,同时把txtInstance改成public即可。

好了,最后一个问题:如何把ChangeTextCommand跟puremvc中的facade实例联系起来呢?

package mvc.controller
{
	import mvc.view.ButtonMediator;
	import mvc.view.TextMediator;

	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.command.SimpleCommand;
	
	import mvc.AppFacade;

	public class AppCommand extends SimpleCommand
	{
		public function AppCommand()
		{
			super();
		}

		public override function execute(inote:INotification):void
		{
			var _main:main=inote.getBody() as main;
			
			facade.registerMediator(new TextMediator(_main.txtResult));
			facade.registerMediator(new ButtonMediator(_main.btnSend));
			
			//注册ChangeTextCommand
			facade.registerCommand(AppFacade.CHANGE_TEXT,ChangeTextCommand);
		}
	}
}

注意加注释的行:facade.registerCommand(AppFacade.CHANGE_TEXT,ChangeTextCommand); 这样就行了,因为AppCommand是在AppFacade中被注册的,所以把ChangeTextCommand的注册加到AppCommand中后,ChangeTextCommand就跟facade联系起来了。

注:本文内容参考了http://bbs.9ria.com/viewthread.php?tid=58719

源文件下载:http://cid-2959920b8267aaca.office.live.com/self.aspx/flex/puremvc^_command.fxp (下载后,直接用Flash Builder 4导入即可)

目录
相关文章
|
5月前
|
Shell Linux 数据安全/隐私保护
Notepad-- 轻量级文本编辑器的安装及基本使用
【7月更文挑战第11天】Notepad-- 轻量级文本编辑器的安装及基本使用
237 3
|
6月前
|
缓存 Java Maven
Spring Boot 启动错误:To display the conditions report re-run your application with ‘debug‘ enable —【已解决】
Spring Boot 启动错误:To display the conditions report re-run your application with ‘debug‘ enable —【已解决】
1279 1
|
7月前
|
Python
RuntimeError: The Werkzeug web server is not designed to run in production. Pass allow_unsafe_werkze
RuntimeError: The Werkzeug web server is not designed to run in production. Pass allow_unsafe_werkze
202 0
|
JavaScript
Pure Admin框架学习笔记---1 (认识它)
Pure Admin框架学习笔记---1 (认识它)
611 0
There is no configured/running web-servers found Please, run any web-configuration and hit the
There is no configured/running web-servers found Please, run any web-configuration and hit the
146 0
|
Web App开发
初学者安装LNMP,提示Failed to start php-fpm.service: Unit is not loaded properly
查看相关文档。文档地址(https://help.aliyun.com/document_detail/97251.html?spm=a2c4g.11186623.6.890.4c087377CLroNl)搭建LNMP 到 5.启动PHP-FPM服务并设置开机自动启动。
4855 0
|
Web App开发 PHP 数据库
symfony3.x 命令行操作Command
Symfony3.x.x通过命令行操作数据库 配置app/config/parameters.yml parameters: database_host: 127.0.0.1 database_port: 3306 database_name: test data.
1904 0