首先,我會用一個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