最简puremvc-LMLPHP工程如下,看来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;
}
}
}
05-11 11:17