@cc-component/cc-ex-component 1.2.3 → 1.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
import { _decorator, Component, Node } from 'cc';
|
|
2
2
|
import { ReferenceComponent } from './ReferenceComponent';
|
|
3
3
|
import { BaseViewModelData } from './BaseViewModelData';
|
|
4
|
+
import { refMap } from './ViewModel';
|
|
5
|
+
import { js } from 'cc';
|
|
4
6
|
const { ccclass, property } = _decorator;
|
|
5
7
|
|
|
6
8
|
@ccclass('BaseReference')
|
|
7
9
|
export class BaseReference extends Component {
|
|
8
|
-
refMap: Map<string, string> = new Map();
|
|
9
10
|
private rc: ReferenceComponent;
|
|
10
11
|
private vmParams: any;
|
|
11
12
|
viewModel: BaseViewModelData;
|
|
12
13
|
param: any;
|
|
13
14
|
initReferenceCollector() {
|
|
14
15
|
this.rc = this.getComponent(ReferenceComponent);
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
const refList = refMap.get(js.getClassName(this)) ?? []
|
|
17
|
+
refList.forEach((value) => { this.defineProperty(value); });
|
|
17
18
|
}
|
|
18
19
|
|
|
19
20
|
onLoadFinish() {
|
|
@@ -21,13 +22,16 @@ export class BaseReference extends Component {
|
|
|
21
22
|
}
|
|
22
23
|
//#endregion
|
|
23
24
|
defineProperty(propertyKey: string) {
|
|
24
|
-
Object.defineProperty(this, propertyKey, { get: () => this.rc
|
|
25
|
+
Object.defineProperty(this, propertyKey, { get: () => this.rc?.get(propertyKey), set: (value) => { }, configurable: true });
|
|
25
26
|
}
|
|
26
27
|
|
|
27
28
|
|
|
28
29
|
refreshUI(vmParams: any) {
|
|
29
30
|
this.vmParams = vmParams;
|
|
30
31
|
}
|
|
32
|
+
protected onDestroy(): void {
|
|
33
|
+
|
|
34
|
+
}
|
|
31
35
|
|
|
32
36
|
|
|
33
37
|
// // 在 test 类中添加
|
|
@@ -112,7 +112,8 @@ export class ReferenceComponent extends Component {
|
|
|
112
112
|
// public get(key: string, type: typeof Node): Node;
|
|
113
113
|
// public get<T extends Component>(key: string, type: new (...args: any[]) => T): T;
|
|
114
114
|
public get(key: string): any {
|
|
115
|
-
if (this._nodeMap
|
|
115
|
+
if (!this._nodeMap) { return null }
|
|
116
|
+
if (this._nodeMap && this._nodeMap.size == 0) this.initNodeMap();
|
|
116
117
|
let node = this._nodeMap.get(key);
|
|
117
118
|
// if (node?.isValid) {
|
|
118
119
|
// if (type as any === Node) {
|
package/assets/core/ViewModel.ts
CHANGED
|
@@ -53,6 +53,8 @@ interface ICollectViewEvents {
|
|
|
53
53
|
const bindingMap = new Map<string, Map<string, IBindingData>>();
|
|
54
54
|
const baseData = 'viewModel'
|
|
55
55
|
const skip = "$"
|
|
56
|
+
export const refMap: Map<string, string[]> = new Map();
|
|
57
|
+
|
|
56
58
|
// const comType = {
|
|
57
59
|
// "cc.Sprite": typeof Sprite,
|
|
58
60
|
// "cc.Label": Label,
|
|
@@ -514,27 +516,25 @@ function updateStatus(data: IBindingData, com: Component, value: any, self: any)
|
|
|
514
516
|
* 响应式数据装饰器
|
|
515
517
|
* 自动将属性转为响应式对象,并使用属性名作为路径前缀
|
|
516
518
|
*/
|
|
517
|
-
export function BindViewModel(
|
|
519
|
+
export function BindViewModel<T extends new () => any>(constructor: T) {
|
|
518
520
|
return function (target: any, propertyKey: string) {
|
|
519
521
|
const originalOnLoad = target.constructor.prototype.onLoad;
|
|
520
522
|
target.constructor.prototype.onLoad = function () {
|
|
521
|
-
//
|
|
522
|
-
const
|
|
523
|
-
|
|
524
|
-
: (initialValue ?? {});
|
|
525
|
-
// 创建响应式对象
|
|
526
|
-
this[propertyKey] = this._makeReactive(initValue, propertyKey);
|
|
523
|
+
// 创建实例并转为响应式
|
|
524
|
+
const instance = new constructor();
|
|
525
|
+
this[propertyKey] = this._makeReactive(instance, propertyKey);
|
|
527
526
|
originalOnLoad?.call(this);
|
|
528
|
-
|
|
529
527
|
};
|
|
530
528
|
};
|
|
531
529
|
}
|
|
532
530
|
|
|
533
531
|
// 定义装饰器
|
|
534
532
|
export function Ref(target: any, propertyKey: string) {
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
if (refMap.
|
|
533
|
+
|
|
534
|
+
const className = js.getClassName(target)
|
|
535
|
+
if (!refMap.has(className)) { refMap.set(className, []) }
|
|
536
|
+
const refList = refMap.get(className)
|
|
537
|
+
if (refList.length <= 0) {
|
|
538
538
|
const originalOnLoad = target.constructor.prototype.onLoad;
|
|
539
539
|
target.constructor.prototype.onLoad = function (this: any) {
|
|
540
540
|
this.initReferenceCollector();
|
|
@@ -542,5 +542,7 @@ export function Ref(target: any, propertyKey: string) {
|
|
|
542
542
|
originalOnLoad?.call(this);
|
|
543
543
|
this.onLoadFinish();
|
|
544
544
|
};
|
|
545
|
+
|
|
545
546
|
}
|
|
547
|
+
refList.push(propertyKey)
|
|
546
548
|
}
|