注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Flex 技术博客

醉里挑灯看剑,梦回吹角连营

 
 
 

日志

 
 

一个PureMVC简单例子  

2008-10-30 15:17:43|  分类: PureMVC |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

如何使用PureMVC写第一个例子

基本步骤如下:

  1. 写一个简单界面Application
  2. ApplicationFacade,并建立消息和command的映射关系
  3. Command,并实现execute方法
  4. Mediator,并列出感兴趣的消息和处理方法
  5. 实现数据处理的Proxy

源码下载

首先我们写一个Application,里面只有一个自定义页面myView,功能很简单,一个buttonclick之后改变一个Text的文本。

首先我们看下面代码,Application在初始化的时候将ApplicationFacade初始化,façade初始化工作具体看ApplicationFacade代码,这里就是简单的注册了command和消息之间的映射关系。

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"

      xmlns:view="example.view.*" creationComplete="init()">

      <mx:Script>

            <![CDATA[

                  import example.ApplicationFacade;

                  private var facade:ApplicationFacade;

                  public function init():void{

                         facade = ApplicationFacade.getInstance();

                         facade.sendNotification(ApplicationFacade.STARTUP,this);

                  }

            ]]>

      </mx:Script>

      <view:MyView id="myView"/>

</mx:Application>

 

//MyView.mxml

Click button的时候抛出一个自定义Notification消息,类似广播

<?xml version="1.0" encoding="utf-8"?>

<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300">

<mx:Script>

      <![CDATA[

            import example.ApplicationFacade;

      ]]>

</mx:Script>

      <mx:TextInput x="0" text="" id="textInput"/>

      <mx:Button y="50" label="Test Click" click="dispatchEvent(new Event(ApplicationFacade.MY_CLICK))"/>

      <mx:Text y="100" text="No Text" id="text"/>

</mx:Canvas>

 

//ApplicationFacade.as

初始化的时候注册commandnotification之间的映射关系

package example

{

      import example.controller.StartupCommand;

     

      import org.puremvc.as3.interfaces.IFacade;

      import org.puremvc.as3.patterns.facade.Facade;

 

      public class ApplicationFacade extends Facade implements IFacade

      {

            public static const STARTUP:String = "STARTUP";

           

            public static const MY_CLICK:String = "MYCLICK";

           

            override protected function initializeController():void{

                  super.initializeController();

                  this.registerCommand(STARTUP,StartupCommand);              

            }

           

            public static function getInstance():ApplicationFacade{

                  if(instance == null) instance = new ApplicationFacade();

                  return instance as ApplicationFacade;

            }

      }

}

 

//MyMediator.as

Mediator负责UI的所有对外事务,包括接受notification和处理收到消息后的时间,并改变界面的元素内容和状态

package example.view

{

      import example.ApplicationFacade;

     

      import flash.events.Event;

      import flash.events.MouseEvent;

     

      import org.puremvc.as3.interfaces.IMediator;

      import org.puremvc.as3.interfaces.INotification;

      import org.puremvc.as3.patterns.mediator.Mediator;

     

      public class MyMediator extends Mediator implements IMediator

      {

            public static const NAME:String = "MyMediator";

            public function MyMediator(mediatorName:String=null, viewComponent:Object=null)

            {

                  super(mediatorName, viewComponent);

                  myView.addEventListener(ApplicationFacade.MY_CLICK,onClickBtn);

            }

           

            public function get myView():MyView{

                  return viewComponent as MyView;

            }

           

            public function onClickBtn(e:Event):void{

                  this.sendNotification(ApplicationFacade.MY_CLICK,myView.textInput.text);

            }

           

            override public function listNotificationInterests():Array{

                  return [

                        ApplicationFacade.MY_CLICK

                  ];

            }

           

            override public function handleNotification(notification:INotification):void{

                  switch(notification.getName())

                  {

                        case ApplicationFacade.MY_CLICK:

                              myView.text.text = notification.getBody() as String;                         

                              break;

                  }

            }

                 

      }

}

 

//StartupCommand.as

启动是的command,它注册了MediatorProxy

package example.controller

{

      import example.model.MyProxy;

      import example.view.MyMediator;

     

      import org.puremvc.as3.interfaces.ICommand;

      import org.puremvc.as3.interfaces.INotification;

      import org.puremvc.as3.patterns.command.SimpleCommand;

 

      public class StartupCommand extends SimpleCommand implements ICommand

      {

            override public function execute(notification:INotification):void{

                  var app:FirstPureMVC = notification.getBody() as FirstPureMVC;

                  facade.registerMediator(new MyMediator(MyMediator.NAME,app.myView));

                  facade.registerProxy(new MyProxy());

            }

           

      }

}

 

//MyProxy.as

数据模型的处理类,可以说对远程数据的请求也可以是对本地数据的维护。可在Command或者Mediator中调用proxy的对外方法。

package example.model

{

      import org.puremvc.as3.interfaces.IProxy;

      import org.puremvc.as3.patterns.proxy.Proxy;

 

      public class MyProxy extends Proxy implements IProxy

      {

            public function MyProxy(proxyName:String=null, data:Object=null)

            {

                  super(proxyName, data);

            }

           

      }

}

  评论这张
 
阅读(5401)| 评论(4)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017