一 , DataModel(数据类)
①:需要继承 bind.BaseBindModel(为了发送属性数据)
②:需要监听的数值需要写setter/getter
③:在setter中使用changeValue方法
changeValue方法参数
1,属性名称
2,与属性对应的字段名称
3,值(要赋的值)
如:
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
|
module app {
/**
* 数据类-成员需要绑定
*/
export class DataTest extends bind.BaseBindModel {
private _name : string =
"Kayer"
;
private _combat : number = 1;
/**
* 设置名称
*/
public set Name( $name : string ){
this
.changeValue<string>(
"Name"
,
"_name"
,$name);
}
/**
* 获取名称
*/
public get Name() : string{
return
this
._name;
}
public set Combat( $combat : number ){
this
.changeValue<number>(
"Combat"
,
"_combat"
,$combat);
}
public get Combat() : number{
return
this
._combat;
}
public constructor() {
super
();
}
}
}
|
二,在View(获取他地方)绑定数值
①:绑定需要使用bind.BindTool类(为了获得发送的属性数据并更新)
②:绑定方案有2种
1,属性绑定 : 直接将新值赋给绑定的值
静态方法 bindProperty<T>
参数5( 最后一个参数 ) : 是否马上用DataModel里面的值为View赋值,默认true
2,回调方法绑定 :
a,回调方法参数为 IBindEventData<T>
静态方法 bindCallBack<T>
参数4(最后一个参数):是否马上用DataModel里面的值为View赋值,默认true
③:销毁
bindProperty<T> 和 bindCallBack<T> 都会返回类 : Bind2Subscriber<T>
Bind2Subscriber<T>提供了销毁方法 : destory(),不需要监听(view关闭时)调用一下
如:
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
/**
* 数据类-成员需要绑定
*/
export class DataTest extends bind.BaseBindModel {
private _name : string =
"Kayer"
;
private _combat : number = 1;
/**
* 设置名称
*/
public set Name( $name : string ){
this
.changeValue<string>(
"Name"
,
"_name"
,$name);
}
/**
* 获取名称
*/
public get Name() : string{
return
this
._name;
}
public set Combat( $combat : number ){
this
.changeValue<number>(
"Combat"
,
"_combat"
,$combat);
}
public get Combat() : number{
return
this
._combat;
}
public constructor() {
super
();
}
}
}
二,在View(获取他地方)绑定数值
①:绑定需要使用bind.BindTool方法
②:绑定方案有2种
1,属性绑定 : 直接将新值赋给绑定的值
静态方法 bindProperty<T>
参数5( 最后一个参数 ) : 是否马上用DataModel里面的值为View赋值,默认
true
2,回调方法绑定 :
a,回调方法参数为 IBindEventData<T>
静态方法 bindCallBack<T>
参数4(最后一个参数):是否马上用DataModel里面的值为View赋值,默认
true
③:销毁
bindProperty<T> 和 bindCallBack<T> 都会返回类 : Bind2Subscriber<T>
Bind2Subscriber<T>提供了销毁方法 : destory(),不需要监听(view关闭时)调用一下
如:
private vName : string =
"CCCC"
;
private vCombat : number = 0;
private dataTest : DataTest =
null
;
private dBind : bind.Bind2Subscriber<string> =
null
;
//不用时需要销毁
private dBind2 : bind.Bind2Subscriber<number> =
null
;
//不用时需要销毁
public constructor() {
super
();
this
.skinName =
"resource/eui_skins/ButtonDemo.exml"
;
egret.log(
"init vName : "
+
this
.vName );
this
.dataTest =
new
DataTest();
egret.log(
"======= 绑定字段(属性) ======"
);
this
.dBind = bind.BindTool.bindProperty(
this
,
"vName"
,
this
.dataTest ,
"Name"
,
true
);
egret.log(
"initChange vName : "
+
this
.vName );
this
.dataTest.Name =
"Aonaufly"
;
egret.log(
"Changed2Listener vName : "
+
this
.vName );
egret.log(
"======= 绑定回调方法 ======"
);
egret.log(
"init vCombat :"
+
this
.vCombat);
this
.dBind2 = bind.BindTool.bindCallBack(
this
.bindCallBack ,
this
.dataTest ,
"Combat"
,
true
);
this
.dataTest.Combat = 7;
}
private bindCallBack( $data : bind.IBindEventData<number> ):void{
if
( $data.$oldValue == undefined ){
//初始化值
egret.log(
"initChange vCombat :"
+ $data.$newValue );
}
else
{
//监听值
egret.log(
"Changed2Listener vCombet :"
);
egret.log(
"oldValue is : "
+ $data.$oldValue);
egret.log(
"newValue is : "
+ $data.$newValue );
}
this
.vCombat = $data.$newValue;
//赋新值
}
/**
* 销毁
*/
public destory() : void{
if
(
this
.dBind !=
null
){
this
.dBind.destory();
this
.dBind =
null
;
}
if
(
this
.dBind2 !=
null
){
this
.dBind2.destory();
this
.dBind2 =
null
;
}
}
|
结果:
附件:http://down.51cto.com/data/2368703
本文转自Aonaufly51CTO博客,原文链接: http://blog.51cto.com/aonaufly/1977030,如需转载请自行联系原作者