此例環境:
flash cs3
鏈接PureMVC類<編輯->首選參數->ActionScript->ActioScript 3.0 設置(加上你的下載的PureMVC類包), PureMVC下載地址>
開始動手嘍~
1, 在flash里準備一下要顯示層的東東:
此例就畫了一個背景方框, 添加一個動態的TextField命名為txt, 然后綁定一個類AppTextField.as;
AppTextField.as里需要接收一個字符串并顯示出來, 些字符串數據就是來自于數據層的(后面有介紹)
復制代碼 代碼如下:
AppTextField.as
package myapp.view.component{
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.text.TextField;
public class AppTextField extends Sprite{
public function AppTextField(str:String):void{
txt.text = str;
}
}
}
2, 準備數據
建立一個DataProxy.as文件用于存放數據(此例只是一個字符串),此類要繼承Proxy實現接口IProxy.
復制代碼 代碼如下:
DataProxy.as
package myapp.model{
import org.puremvc.as3.patterns.proxy.Proxy;
import org.puremvc.as3.interfaces.IProxy;
public class DataProxy extends Proxy implements IProxy{
private var _info:String;
static public const NAME:String = "DataProxy";
public function DataProxy() {
super(NAME);
return;
}
public function get info():String {
return "Ok! Is very good!";
}
}
}
3.注冊啟動命令(復合):StartupCommand和兩個子命令ModelPrepCommand, ViewPrepCommand(數據初始化和顯示層初始化)
復制代碼 代碼如下:
StartupCommand.as
package myapp.controller{
import org.puremvc.as3.patterns.command.MacroCommand;
public class StartupCommand extends MacroCommand {
// 程序開始時執行的 MacroCommand.
public function StartupCommand() {
return;
}
//添加子Command 初始化 MacroCommand.
override protected function initializeMacroCommand():void {
//以下兩個命令按先進先出順序執行;
addSubCommand(ModelPrepCommand);
addSubCommand(ViewPrepCommand);
return;
}
}
}
復制代碼 代碼如下:
ModelPrepCommand.as
package myapp.controller{
import myapp.model.*;
import org.puremvc.as3.interfaces.ICommand;
import org.puremvc.as3.interfaces.INotification;
import org.puremvc.as3.patterns.command.SimpleCommand;
public class ModelPrepCommand extends SimpleCommand implements ICommand {
//創建 Proxy 對象,并注冊;
public function ModelPrepCommand() {
return;
}
//由MacroCommand 調用;
public override function execute(sender:INotification):void {
facade.registerProxy(new DataProxy());
return;
}
}
}
復制代碼 代碼如下:
ViewPrepCommand.as
package myapp.controller{
import myapp.view.*;
import org.puremvc.as3.interfaces.ICommand;
import org.puremvc.as3.interfaces.INotification;
import org.puremvc.as3.patterns.command.SimpleCommand;
public class ViewPrepCommand extends SimpleCommand implements ICommand {
public function ViewPrepCommand() {
return;
}
// 創建 Mediator, 并把它們注冊到View;
public override function execute(sender:INotification):void {
var obj:Main;
obj = sender.getBody() as Main;
facade.registerMediator(new AlertMediator(obj));
return;
}
}
}
4.創建Mediator對象類AlertMediator.as用于操作具體顯示層組件(此例是AppTextField.as)
復制代碼 代碼如下:
AlertMediator.as
package myapp.view{
import myapp.MyappFacade;
import myapp.model.DataProxy;
import myapp.view.component.AppTextField;
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.text.TextField;
import flash.events.Event;
import org.puremvc.as3.interfaces.IMediator;
import org.puremvc.as3.patterns.mediator.Mediator;
public class AlertMediator extends Mediator implements IMediator {
private var data:DataProxy;
static public const NAME:String = "AlertMediator";
public function AlertMediator(obj:Object) {
super(NAME, obj);
data = facade.retrieveProxy(DataProxy.NAME) as DataProxy;
var t:AppTextField = new AppTextField(data.info);
main.addChild(t);
}
function get main():Main {
return viewComponent as Main;
}
}
}
5.創建建立Command與Notification之間的映射關系類MyappFacade.as
復制代碼 代碼如下:
MyappFacade.as
package myapp{
import org.puremvc.as3.interfaces.IFacade;
import org.puremvc.as3.patterns.facade.Facade;
import myapp.view.*;
import myapp.model.*;
import myapp.controller.*;
// MyApp 程序的 Facade 類
public class MyappFacade extends Facade implements IFacade {
//定義 Notification (通知)常量
public static const STARTUP:String = "startup";
public static const LOGIN:String = "login";
//得到ApplicationFacade 單例的工廠方法
public static function getInstance():MyappFacade {
if ( instance == null ) {
instance = new MyappFacade( );
}
return instance as MyappFacade;
}
//注冊 Command,建立Command 與 Notification 之間的映射
override protected function initializeController( ):void {
super.initializeController();
registerCommand( STARTUP, StartupCommand );
}
//啟動 PureMVC,在應用程序中調用此方法,并傳遞應用程序本身的引用
public function startup( app:Main ):void {
sendNotification( STARTUP, app );
}
}
}
6.創建主文檔類Main.as(啟動命名)
復制代碼 代碼如下:
Main.as
package {
import myapp.*;
import flash.display.*;
public class Main extends Sprite {
private var facade:MyappFacade;
public function Main() {
facade = MyappFacade.getInstance();
//執行開始命令;
facade.startup(this);
return;
}
}
}
可能寫的不清不楚啊, 哈~~ 有問題請留言一起探討吧!
文件打包下載(www.companysz.com)