flash PureMVC 使用例子
此例环境:
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 );
}
}
}
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.jb51.net)