Egret 中使用puremvc
在上篇中介绍了如何将puremvc第三方库导入到项目中,在本文中将介绍如何在项目中启动puremvc。
首先定义好统一消息管理文件,方便修改和维护1
2
3
4
5
6module Jun {
export const NOTICE = {
"STARTUP" : Symbol(),//启动MVC
"CLIENT_INITED" : Symbol(),//客户端初始化完成
}
}
首先需要用ts自定义实现ApplicationFacade的单例,实现如下:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33/**
* @author Junix
* @desc 引擎面门,用于启动整个项目
**/
module Jun {
export class ApplicationFacade extends puremvc.Facade implements puremvc.IFacade{
public constructor(){
super();
}
public static getInstance():ApplicationFacade{
if (this.instance == null) {
this.instance = new ApplicationFacade();
}
return <ApplicationFacade><any> (this.instance);
}
public initializeController():void{
super.initializeController();
this.registerCommand(NOTICE.STARTUP, StartupCommand );
}
/**
* 启动PureMVC,在应用程序中调用此方法,并传递应用程序本身的引用
* @param rootView:PureMVC应用程序的根视图root,包含其它所有的View Componet
*/
public startUp(rootView:egret.DisplayObjectContainer):void{
this.sendNotification(NOTICE.STARTUP, stage);
this.removeCommand(NOTICE.STARTUP);
}
}
}
StartupCommand的实现1
2
3
4
5
6
7
8
9
10
11
12
13
14/**
* @author Junix
* @desc StartupCommand 启动命令,项目启动后用于初始化M,V,C 命令
**/
module mx {
export class StartupCommand extends puremvc.MacroCommand{
//携带的消息会依次传递给子消息
public initializeMacroCommand():void{
this.addSubCommand(ModelPrepCommand);//model管理器
this.addSubCommand(ViewPrepCommand);//view管理器
this.addSubCommand(ControllerPrepCommand);//controller管理器
}
}
}
对应的 ModelPrepCommand、ViewPrepCommand、ControllerPrepCommand的实现1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36/**
* @author Junix
* @desc 数据层命令,注册所有的model
**/
module Jun {
export class ModelPrepCommand extends puremvc.SimpleCommand implements puremvc.ICommand{
public execute(notification : puremvc.INotification) : void{
this.facade.registerProxy(new GameProxy());
}
}
}
/*
* @author Junix
* @desc 显示层命令,V层只注册跟容器的mediator 具体场景的mediator可去对应的view下注册
**/
module Jun {
export class ViewPrepCommand extends puremvc.SimpleCommand implements puremvc.ICommand{
public execute(notification:puremvc.INotification):void{
let main = notification.getBody();
this.facade.registerMediator(new MainMediator(main));
}
}
}
/**
* @author Junix
* @desc 控制层命令, 注册所有command
**/
module Jun {
export class ControllerPrepCommand extends puremvc.SimpleCommand implements puremvc.ICommand{
public execute(notification:puremvc.INotification):void{
(new GameCommand()).register();
}
}
}
准备工作完成,为了验证是否正确启动了puremvc,我们单独注册了GameCommand来监听CLIENT_INITED这一则消息,并由gameproxy做出对应处理1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35//GameCommand 监听CLIENT_INITED
module Jun{
export class GameCommand extends puremvc.SimpleCommand implements puremvc.ICommand{
public register() : void {
this.facade.registerCommand(NOTICE.CLIENT_INITED,GameCommand);
}
public execute(notification:puremvc.INotification):void{
let data = notification.getBody();
let name = notification.getName();
let proxy = <GameProxy><any> this.facade.retrieveProxy(GameProxy.NAME);
switch(name){
case NOTICE.CLIENT_INITED:
proxy.init_game();
break;
}
}
}
}
//GameProxy 相关数据处理
module Jun{
export class GameProxy extends puremvc.Proxy implements puremvc.IProxy{
public static NAME : string = 'GameProxy';
public constructor(){
super(GameProxy.NAME);
}
//对应处理
public init_game() : void{
console.log('初始成功');
}
}
}
在Main.ts中初始化,如果你有另写过游戏里自定义容器的话,则将该容器作为根视图传入startUp()中1
2
3
4
5//Main.ts创建主界面时
//初始化MVC结构
Jun.ApplicationFacade.getInstance().startUp(this);
//发送CLIENT_INITED消息
Jun.ApplicationFacade.getInstance().sendNotification(jun.NOTICE.CLIENT_INITED);
最后运行项目,在输出控制台里有输出说明puremvc框架成功启动并使用