flash PureMVC 使用例子
更新時(shí)間:2009年01月10日 23:11:02 作者:
昨天下了PureMVC, 看了一天文檔, 迷茫啊, 找例子也沒(méi)找到, 后來(lái)破解了一個(gè)看了一下, 大概地寫了一個(gè)簡(jiǎn)單的用例, 先算是學(xué)習(xí)筆記吧, 先把這最簡(jiǎn)單的例子做法寫一下吧( 也不知道這種理解是否正確哦~~汗一個(gè)!)
此例環(huán)境:
flash cs3
鏈接PureMVC類<編輯->首選參數(shù)->ActionScript->ActioScript 3.0 設(shè)置(加上你的下載的PureMVC類包), PureMVC下載地址>
開(kāi)始動(dòng)手嘍~
1, 在flash里準(zhǔn)備一下要顯示層的東東:
此例就畫了一個(gè)背景方框, 添加一個(gè)動(dòng)態(tài)的TextField命名為txt, 然后綁定一個(gè)類AppTextField.as;
AppTextField.as里需要接收一個(gè)字符串并顯示出來(lái), 些字符串?dāng)?shù)據(jù)就是來(lái)自于數(shù)據(jù)層的(后面有介紹)
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, 準(zhǔn)備數(shù)據(jù)
建立一個(gè)DataProxy.as文件用于存放數(shù)據(jù)(此例只是一個(gè)字符串),此類要繼承Proxy實(shí)現(xiàn)接口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.注冊(cè)啟動(dòng)命令(復(fù)合):StartupCommand和兩個(gè)子命令ModelPrepCommand, ViewPrepCommand(數(shù)據(jù)初始化和顯示層初始化)
StartupCommand.as
package myapp.controller{
import org.puremvc.as3.patterns.command.MacroCommand;
public class StartupCommand extends MacroCommand {
// 程序開(kāi)始時(shí)執(zhí)行的 MacroCommand.
public function StartupCommand() {
return;
}
//添加子Command 初始化 MacroCommand.
override protected function initializeMacroCommand():void {
//以下兩個(gè)命令按先進(jìn)先出順序執(zhí)行;
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 {
//創(chuàng)建 Proxy 對(duì)象,并注冊(cè);
public function ModelPrepCommand() {
return;
}
//由MacroCommand 調(diào)用;
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;
}
// 創(chuàng)建 Mediator, 并把它們注冊(cè)到View;
public override function execute(sender:INotification):void {
var obj:Main;
obj = sender.getBody() as Main;
facade.registerMediator(new AlertMediator(obj));
return;
}
}
}
4.創(chuàng)建Mediator對(duì)象類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.創(chuàng)建建立Command與Notification之間的映射關(guān)系類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;
}
//注冊(cè) Command,建立Command 與 Notification 之間的映射
override protected function initializeController( ):void {
super.initializeController();
registerCommand( STARTUP, StartupCommand );
}
//啟動(dòng) PureMVC,在應(yīng)用程序中調(diào)用此方法,并傳遞應(yīng)用程序本身的引用
public function startup( app:Main ):void {
sendNotification( STARTUP, app );
}
}
}
6.創(chuàng)建主文檔類Main.as(啟動(dòng)命名)
Main.as
package {
import myapp.*;
import flash.display.*;
public class Main extends Sprite {
private var facade:MyappFacade;
public function Main() {
facade = MyappFacade.getInstance();
//執(zhí)行開(kāi)始命令;
facade.startup(this);
return;
}
}
}
可能寫的不清不楚啊, 哈~~ 有問(wèn)題請(qǐng)留言一起探討吧!
文件打包下載(www.dbjr.com.cn)
flash cs3
鏈接PureMVC類<編輯->首選參數(shù)->ActionScript->ActioScript 3.0 設(shè)置(加上你的下載的PureMVC類包), PureMVC下載地址>
開(kāi)始動(dòng)手嘍~
1, 在flash里準(zhǔn)備一下要顯示層的東東:
此例就畫了一個(gè)背景方框, 添加一個(gè)動(dòng)態(tài)的TextField命名為txt, 然后綁定一個(gè)類AppTextField.as;
AppTextField.as里需要接收一個(gè)字符串并顯示出來(lái), 些字符串?dāng)?shù)據(jù)就是來(lái)自于數(shù)據(jù)層的(后面有介紹)
復(fù)制代碼 代碼如下:
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, 準(zhǔn)備數(shù)據(jù)
建立一個(gè)DataProxy.as文件用于存放數(shù)據(jù)(此例只是一個(gè)字符串),此類要繼承Proxy實(shí)現(xiàn)接口IProxy.
復(fù)制代碼 代碼如下:
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.注冊(cè)啟動(dòng)命令(復(fù)合):StartupCommand和兩個(gè)子命令ModelPrepCommand, ViewPrepCommand(數(shù)據(jù)初始化和顯示層初始化)
復(fù)制代碼 代碼如下:
StartupCommand.as
package myapp.controller{
import org.puremvc.as3.patterns.command.MacroCommand;
public class StartupCommand extends MacroCommand {
// 程序開(kāi)始時(shí)執(zhí)行的 MacroCommand.
public function StartupCommand() {
return;
}
//添加子Command 初始化 MacroCommand.
override protected function initializeMacroCommand():void {
//以下兩個(gè)命令按先進(jìn)先出順序執(zhí)行;
addSubCommand(ModelPrepCommand);
addSubCommand(ViewPrepCommand);
return;
}
}
}
復(fù)制代碼 代碼如下:
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 {
//創(chuàng)建 Proxy 對(duì)象,并注冊(cè);
public function ModelPrepCommand() {
return;
}
//由MacroCommand 調(diào)用;
public override function execute(sender:INotification):void {
facade.registerProxy(new DataProxy());
return;
}
}
}
復(fù)制代碼 代碼如下:
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;
}
// 創(chuàng)建 Mediator, 并把它們注冊(cè)到View;
public override function execute(sender:INotification):void {
var obj:Main;
obj = sender.getBody() as Main;
facade.registerMediator(new AlertMediator(obj));
return;
}
}
}
4.創(chuàng)建Mediator對(duì)象類AlertMediator.as用于操作具體顯示層組件(此例是AppTextField.as)
復(fù)制代碼 代碼如下:
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.創(chuàng)建建立Command與Notification之間的映射關(guān)系類MyappFacade.as
復(fù)制代碼 代碼如下:
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;
}
//注冊(cè) Command,建立Command 與 Notification 之間的映射
override protected function initializeController( ):void {
super.initializeController();
registerCommand( STARTUP, StartupCommand );
}
//啟動(dòng) PureMVC,在應(yīng)用程序中調(diào)用此方法,并傳遞應(yīng)用程序本身的引用
public function startup( app:Main ):void {
sendNotification( STARTUP, app );
}
}
}
6.創(chuàng)建主文檔類Main.as(啟動(dòng)命名)
復(fù)制代碼 代碼如下:
Main.as
package {
import myapp.*;
import flash.display.*;
public class Main extends Sprite {
private var facade:MyappFacade;
public function Main() {
facade = MyappFacade.getInstance();
//執(zhí)行開(kāi)始命令;
facade.startup(this);
return;
}
}
}
可能寫的不清不楚啊, 哈~~ 有問(wèn)題請(qǐng)留言一起探討吧!
文件打包下載(www.dbjr.com.cn)
相關(guān)文章
as3+xml+asp+access做的有獎(jiǎng)問(wèn)答
as3+xml+asp+access做的有獎(jiǎng)問(wèn)答實(shí)現(xiàn)代碼2009-02-02ActionScript 3.0中用XMLSocket與服務(wù)器通訊程序(源碼)
一個(gè)簡(jiǎn)單的基于XMLSocket的封裝類2009-02-02AS3 navigateToURL導(dǎo)致ExternalInterface 執(zhí)行失敗問(wèn)題
AS3 navigateToURL導(dǎo)致ExternalInterface 執(zhí)行失敗問(wèn)題2009-02-02Actionscript 3.0中Singleton實(shí)現(xiàn) 修正篇
說(shuō)明:上一篇"一個(gè)簡(jiǎn)單的Actionscript的單態(tài)模式類"的實(shí)現(xiàn)在Actionscript中報(bào)錯(cuò),具體原因會(huì)在這篇Blog中詳細(xì)說(shuō)明。2009-02-02