LView Class
视图View
视图是一个LSprite的子对象。视图必须放到Views文件夹内,视图的名字以View结尾。
控制器Controller一共继承自两个类LMvcObject,LSprite,所以除了可以使用LMvcObject的函数之外,还拥有LSprite类addChild等所有属性和函数,所以LView是一个可视化的类。
※由于视图View的construct函数是在控制器Controller的construct函数之后执行,所以View中使用控制器的一些属性或者从控制器传来值的时候,是无法在construct中使用的,那么一般可以给控制器Controller添加LEvent.COMPLETE事件,像下面的demo一样这么做。
Constructor
LView
()
public
Example:
//控制器中代码
ExampleController.prototype.construct = function(){
//在控制器的construct中读取必要的素材和类,比如下面读取几个模型,然后回调modelLoadComplete
this.load.model(["User/User","User/Character"],this.modelLoadComplete);
}
ExampleController.prototype.modelLoadComplete = function(){
//通知视图,控制器准备工作已经结束
this.dispatchEvent(LEvent.COMPLETE);
//给控制器添加一个值,然后通知视图,这样视图中就可以使用它
this.setValue("myname","testName");
this.dispatchEvent(LController.NOTIFY);
}
//视图中代码
ExampleView.prototype.construct = function(){
//在视图的construct中给控制器添加LEvent.COMPLETE事件
this.controller.addEventListener(LEvent.COMPLETE, this.init.bind(this));
}
ExampleView.prototype.init = function(){
//等待控制器准备工作结束后执行
this.txtName = new LTextField();
this.x = 100;
this.y = 100;
this.addChild(this.txtName);
}
ExampleView.prototype.updateView = function(){
//将控制器中添加的值赋给this.txtName
this.txtName.text = this.controller.getValue("myname");
trace(this.txtName.text);//out: testName
}
Methods
construct
()
public
控制器,模型,视图初始化结束后都会直接调用此函数,如果有想要在各个模块初期化结束后运行的代码,可以写在construct函数内
本MVC框架中的construct函数运行顺序为,模型model的construct函数 > 视图view函数 > 控制器的construct。
loadMvc
(
public
-
name
-
callback
读取一组MVC,包括控制器Controller,模型Model,视图View
Parameters:
-
name
String控制器的名称中去除Controller的部分.
-
callback
Function回调函数,当MVC的三个文件读取完之后,会自动调用此函数
Example:
ExampleController.prototype.construct = function(){
this.loadMvc("Logo",self.logoLoad);
}
ExampleController.prototype.logoLoad = function(){
var logo = new LogoController();
this.view.addChild(logo.view);
}
updateView
()
public
在自己控制器发送LController.NOTIFY消息的时候,则自动调用自己的updateView函数
Example:
//控制器中代码
ExampleController.prototype.construct = function(){
this.setValue("myname","testName");
this.dispatchEvent(LController.NOTIFY);
}
//视图中代码
ExampleView.prototype.updateView = function(){
trace(this.controller.getValue("myname"));//out: testName
}