flash PureMVC 使用例子
更新时间:2009年01月10日 23:11:02 作者:
昨天下了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 );
}
}
}
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.jb51.net)
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.jb51.net)
相关文章
ActionScript 3.0中用XMLSocket与服务器通讯程序(源码)
一个简单的基于XMLSocket的封装类2009-02-02AS3 navigateToURL导致ExternalInterface 执行失败问题
AS3 navigateToURL导致ExternalInterface 执行失败问题2009-02-02Actionscript 3.0中Singleton实现 修正篇
说明:上一篇"一个简单的Actionscript的单态模式类"的实现在Actionscript中报错,具体原因会在这篇Blog中详细说明。2009-02-02
最新评论