zoukankan      html  css  js  c++  java
  • 最简puremvc

    工程如下,看来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;
            }
        }
    }
  • 相关阅读:
    【BIEE】01_下载安装BIEE(Business Intelligence)11g 11.1.1.9.0
    【Excle数据透视表】如何按照地区交替填充背景颜色
    【Excle数据透视表】如何利用图标集将销售数据划分为五个等级
    【Excle数据透视表】如何将价格小于5000的显示为红色“不达标”
    【Excle数据透视表】如何让字段标题不显示“求和项”
    【Excle】如何隐藏数据透视表中的错误值
    使用虚拟机运行Ubuntu时,主机与宿主机共享文件的方法。
    mount命令汇总
    虚拟机网络模式
    linux(虚拟机中)与windows共享文件两种方法
  • 原文地址:https://www.cnblogs.com/as3lib/p/5166619.html
Copyright © 2011-2022 走看看