优化MVVM代码

This commit is contained in:
dgflash
2025-09-28 16:18:48 +08:00
parent a20c8ef68c
commit 6ae2262743
2 changed files with 9 additions and 17 deletions

View File

@@ -2,8 +2,7 @@ import { LanguageData } from "../gui/language/LanguageData";
/**
* 数值格式化函数, 通过语义解析自动设置值的范围
* //整数
* 1:def(0)//显示一个默认值
* 1:def(0) // 显示一个默认值
*/
class StringFormat {
deal(value: number | string, format: string): string {

View File

@@ -1,15 +1,16 @@
import { Component, _decorator } from 'cc';
import { GameComponent } from '../../module/common/GameComponent';
import { VM } from './ViewModel';
import { VMBase } from './VMBase';
const { ccclass, help, executionOrder } = _decorator;
/**
* 提供VM环境控制旗下所有VM节点
* 一般用于 非全局的 VM绑定,VM 环境与 组件紧密相连
* 一般用于 非全局的 VM绑定,VM 环境与组件紧密相连
* Prefab 模式绑定)
* VMParent 必须必其他组件优先执行
* v0.1 修复bug 现在可以支持 Parent 嵌套 (但是注意性能问题,不要频繁嵌套)
* 现在可以支持 Parent 嵌套(但是注意性能问题,不要频繁嵌套)
*/
@ccclass
@executionOrder(-1)
@@ -21,9 +22,6 @@ export default class VMParent extends GameComponent {
/** 需要绑定的私有数据 */
protected data: any = {};
/** VM 管理 */
VM = VM;
/**
* [注意]不能直接覆盖此方法,如果需要覆盖。
* 只能在该方法内部调用父类的实现
@@ -32,7 +30,6 @@ export default class VMParent extends GameComponent {
* super.onLoad();
* }
* ```
*
*/
onLoad() {
if (this.data == null) return;
@@ -52,14 +49,10 @@ export default class VMParent extends GameComponent {
}
/**在 onLoad 完成 和 start() 之前调用,你可以在这里进行初始化数据等操作 */
protected onBind() {
}
protected onBind() { }
/**在 onDestroy() 后调用,此时仍然可以获取绑定的 data 数据*/
protected onUnBind() {
}
protected onUnBind() { }
private replaceVMPath(comp: Component, tag: string) {
// @ts-ignore
@@ -88,13 +81,13 @@ export default class VMParent extends GameComponent {
/** 未优化的遍历节点获取VM 组件 */
private getVMComponents() {
let comps = this.node.getComponentsInChildren('VMBase');
let parents = this.node.getComponentsInChildren('VMParent').filter(v => v.uuid !== this.uuid); // 过滤掉自己
let comps = this.node.getComponentsInChildren(VMBase);
let parents = this.node.getComponentsInChildren(VMParent).filter(v => v.uuid !== this.uuid); // 过滤掉自己
//过滤掉不能赋值的parent
let filters: any[] = [];
parents.forEach((node: Component) => {
filters = filters.concat(node.getComponentsInChildren('VMBase'));
filters = filters.concat(node.getComponentsInChildren(VMBase));
})
comps = comps.filter((v) => filters.indexOf(v) < 0);