博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
最简puremvc
阅读量:6202 次
发布时间:2019-06-21

本文共 6017 字,大约阅读时间需要 20 分钟。

工程如下,看来sendNotification是像comand和mediator发消息的

package{    import flash.display.Sprite;    import flash.events.Event;    import flash.text.TextField;        public class Main extends Sprite    {        public var txt:TextField;        public var btn:MyButton;        public function Main()        {            if(stage) init();            else addEventListener(Event.ADDED_TO_STAGE,init);        }        private function init(e:Event=null):void        {            this.removeEventListener(Event.ADDED_TO_STAGE,init);            txt = new TextField();            txt.text = "hellopuremvc";            this.addChild(txt);            btn = new MyButton();            btn.y = 90;            this.addChild(btn)                        MyFacade.getInstance().startup(this);        }    }}
package{    import controller.MyCommand;    import org.puremvc.as3.interfaces.IFacade;    import org.puremvc.as3.patterns.facade.Facade;    public class MyFacade extends Facade implements IFacade    {        public static const STARTUP:String = "startup";        public static const CHANGETEXT:String = "changetext";        public static const CHANGECOMMAND:String = "changecommand";        public function MyFacade()        {        }        public static function getInstance():MyFacade        {            if(instance == null){                instance = new MyFacade();            }            return instance as MyFacade;        }        override protected function initializeController():void        {            super.initializeController();            registerCommand(STARTUP,MyCommand);                    }        public function startup(_main:Main):void        {            //启动puremvc环境            sendNotification(STARTUP,_main);        }    }}
package controller{    import org.puremvc.as3.interfaces.INotification;    import org.puremvc.as3.patterns.command.SimpleCommand;    public class ChangeCommand extends SimpleCommand    {        public function ChangeCommand()        {            super();        }        override public function execute(note:INotification):void{            trace("ChangeCommand执行了");        }    }}
package controller{    import model.MyProxy;    import org.puremvc.as3.interfaces.INotification;    import org.puremvc.as3.patterns.command.SimpleCommand;    import view.BtnMediator;    import view.TextMediator;    public class MyCommand extends SimpleCommand    {        public function MyCommand()        {            super();        }        override public function execute(note:INotification):void        {            var _main:Main = note.getBody() as Main;            this.facade.registerProxy(new MyProxy());            this.facade.registerMediator(new TextMediator(_main.txt));            this.facade.registerMediator(new BtnMediator(_main.btn));            this.facade.registerCommand(MyFacade.CHANGECOMMAND,ChangeCommand);        }    }}
package model{    import flash.events.Event;    import flash.events.IOErrorEvent;    import flash.net.URLLoader;    import flash.net.URLRequest;    import org.puremvc.as3.interfaces.IProxy;    import org.puremvc.as3.patterns.proxy.Proxy;    public class MyProxy extends Proxy implements IProxy    {        public static const NAME:String = "MyProxy";        public function MyProxy(proxyName:String = null,data:Object = null)        {            super(NAME,data);            var loader:URLLoader = new URLLoader();            loader.addEventListener(Event.COMPLETE,onComplete);            loader.addEventListener(IOErrorEvent.IO_ERROR,onError);            loader.load(new URLRequest("data.xml"));        }        private function onComplete(e:Event):void        {            var xml:XML = XML(e.target.data);            this.setData(xml);        }        private function onError(e:IOErrorEvent):void        {            trace("数据获取失败");        }    }}
package view{    import flash.display.SimpleButton;    import flash.events.MouseEvent;        import org.puremvc.as3.interfaces.IMediator;    import org.puremvc.as3.patterns.mediator.Mediator;    public class BtnMediator extends Mediator implements IMediator    {        public static const NAME:String = "BtnMediator";        public function BtnMediator(viewComponent:SimpleButton)        {            super(NAME,viewComponent);            btn.addEventListener(MouseEvent.CLICK,onClick);        }        private function onClick(evt:MouseEvent):void        {            sendNotification(MyFacade.CHANGETEXT);            sendNotification(MyFacade.CHANGECOMMAND);            //sendNotification 看来是给command和mediator发消息的        }        public function get btn():SimpleButton        {            return viewComponent as SimpleButton;        }    }}
package view{    import flash.text.TextField;        import model.MyProxy;        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:TextField)        {            super(NAME,viewComponent);        }        override public function listNotificationInterests():Array        {            return [MyFacade.CHANGETEXT,MyFacade.CHANGECOMMAND];//对MyFacade.CHANGETEXT感兴趣        }        override public function handleNotification(note:INotification):void        {            switch(note.getName()){                case MyFacade.CHANGETEXT:                    var proxy:MyProxy = facade.retrieveProxy(MyProxy.NAME) as MyProxy;                    var xml:XML = XML(proxy.getData());                    txt.text = xml.msg;                                        break;                case MyFacade.CHANGECOMMAND:                    trace("mediator侦听command的命令也执行了");                    break;            }        }        public function get txt():TextField        {            return viewComponent as TextField;        }    }}

 

转载于:https://www.cnblogs.com/as3lib/p/5166619.html

你可能感兴趣的文章
java的HashMap 原理
查看>>
宿主进程 [*.vshost.exe] & [*.vshost.exe.config]
查看>>
JS自学笔记01
查看>>
cin、cin.get()、cin.getline()、getline()、gets()等函数的用法
查看>>
ibatis源码学习2_初始化和配置文件解析
查看>>
剖析一个由sendfile引发的linux内核BUG
查看>>
玩转Red5+Flex(4)—— Red5配置文件之解说
查看>>
View
查看>>
NSString 小技巧
查看>>
python爬取智联招聘职位信息(单进程)
查看>>
archlinux/manjaro mysql安装[linux]
查看>>
用普通网络摄像头模拟Leap Motion追踪性能
查看>>
亲身经历——大体量公司能为程序员的生涯带来什么帮助?
查看>>
MVC、MVVM
查看>>
cocos2dx 3.x (单选,多选,复选checkBox按钮的实现) RadioButton
查看>>
Maven 插件打包部署项目
查看>>
最老程序员创业札记:全文检索、数据挖掘、推荐引擎应用52
查看>>
list实现大整数加法
查看>>
记录一次批量处理文档的过程
查看>>
Webstorm2016使用技巧——SVN插件使用(svnToolBox)
查看>>