Flex中PureMVC学习

首先,我会用一个Demo来说明我的学习过程.

 

分析:

 

1.选择自己需要进入的网站类型;

 

2.如果选择 Flex,哪么就需要登陆.

 

以上是我现在Demo的雏形.

 

注:pureMVC我就不讲了.不懂的可以去看相关的资料.现在有中文版的资 料.

 

第一步:建立好工程,加载进pureMVC的框架,建立好文件结构.

 

如下图

 

 

 

第一步:

 

首先是注册相关的通知,为了方便管理,我将 所有的通知均放在一个as文件中,命名为AppConstance.as

 

代码如下:

 

 1package myApp

 2{

 3    public class AppConstance

 4    {

 5        public function AppConstance()

 6        {

 7        }

 8        //注册通知;

 9        public static const START:String = "start";

10        public static const TO_CHOOSE:String = "to_choose";

11        public static const CHOOSE:String = "choose";

12        public static const TO_LOGIN:String = "to_login";

13        public static const LOGIN:String = "login";

14        public static const LOGIN_SUCCESS:String = "login_success";

15        public static const LOGIN_FAILURE:String = "login_failure"

16        public static const TO_WELCOME:String = "to_welcome";

17        public static const WELCOME:String = "welcome";

18

19    }

20}

 

第二步:

 

在PureMVC中Facade只有一个,而且永远不会 被实例化,本程序中Facade命名为:ApplicationFacade.as

 

代码如下

 

package myApp

{

    import org.puremvc.as3.interfaces.IFacade;

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

    import myApp.AppConstance;

    import myApp.Controller.startCommand;

 

    public class ApplicationFacade extends Facade implements IFacade

    {

        public function ApplicationFacade()

        {

            super();

        }

 

        

        public static function getInstance():ApplicationFacade{

            if(instance == null){

                instance = new ApplicationFacade();

            }

            return instance as ApplicationFacade;

        }

 

        

        override protected function initializeController():void{

            super.initializeController();

            registerCommand(AppConstance.START,startCommand);

        }

 

        public function start(app:myApp):void{

            sendNotification(AppConstance.START,app,"myweb");

        }

    }

}

 

解释下:

 

public static function getInstance():ApplicationFacade{

            if(instance == null){

                instance = new ApplicationFacade();

            }

            return instance as ApplicationFacade;

        }

 

这句是获得ApplicationFacade单列工厂,如果不存在则新创建一个,然后返回一个 ApplicationFacade.

 

override protected function initializeController():void{

            super.initializeController();

            registerCommand(AppConstance.START,startCommand);

        }

 

初始化Controller,并注册了START这个通知与startCommand的映射,注意,通知必须有相关的 Command,Mediator对应.

 

然后创建开始函数:

 

public function start(app:myApp):void{

            sendNotification(AppConstance.START,app,"myweb");

        }

 

这里我传了一个参数:app:myApp,意思是传的参数app是myApp类型,myApp指主文件myApp.mxml.

 

到此 ApplicationFacade创建完成,实现了开始函数:start(),单列工厂:getInstance(),还有初始化controller:initializeController();

 

myApp.mxml

 

myApp属于一个空的Application,在程序开始运行时就需要去执行start();

 

见代 码:

 

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

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="intApp()">

    <mx:Script>

        <![CDATA[

            import myApp.ApplicationFacade;

            public function intApp():void{

                var facade:ApplicationFacade = ApplicationFacade.getInstance();

                facade.start(this);

            }

        ]]>

    </mx:Script>

</mx:Application>

 

导入ApplicationFacade类. 声明一个函数为intApp()并在页面加载完成后执行:craationComplete = "intApp()";

 

声明了一个变量facade,类型是ApplicationFacade,并执行start(),这样程序就启动了..

 

第四 步:

 

startCommand:

 

在ApplicationFacade中,我声明了AppConstance.START,这个通知与startCommand的映射,即,在程序开始执行 时,就需要去执行startCommand

 

代码:

 

package myApp.Controller

{

    import myApp.AppConstance;

    import myApp.Model.userProxy;

    import myApp.View.chooseMediator;

    import myApp.View.loginMediator;

 

    import org.puremvc.as3.interfaces.ICommand;

    import org.puremvc.as3.interfaces.INotification;

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

 

    public class startCommand extends SimpleCommand implements ICommand

    {

        public function startCommand()

        {

            super();

        }

 

        override public function execute(notification:INotification):void

        {

            var body:Object = notification.getBody();

            var type:String = notification.getType();

            if(type == "myweb"){

                this.facade.registerCommand(AppConstance.LOGIN,loginCommand);

                this.facade.registerMediator(new chooseMediator(body));

                this.facade.registerMediator(new loginMediator(body));

                this.facade.registerProxy(new userProxy());

 

                this.sendNotification(AppConstance.TO_CHOOSE,body)

 

            }else{

                trace("不是本站");

            }

        }

 

    }

}

 

 

本文来自编程入门网:http://www.bianceng.cn/webkf/Flex/201107/27702_3.htm

posted on 2012-04-22 11:33  ycty  阅读(196)  评论(0编辑  收藏  举报