前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >puremvc框架之Command

puremvc框架之Command

作者头像
菩提树下的杨过
发布2018-01-23 15:41:40
1.2K0
发布2018-01-23 15:41:40
举报

在前一篇 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导入即可)

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2010-07-31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档