1. 优化MVVM组件代码

2. VMLable添加动态绑定组件的方法
This commit is contained in:
dgflash
2025-09-22 11:20:43 +08:00
parent cc5f86cfda
commit bc1a91b31d
6 changed files with 36 additions and 20 deletions

View File

@@ -30,7 +30,7 @@ export class JsonOb<T> {
private _callback;
/**对象属性劫持 */
/** 对象属性劫持 */
private observe<T>(obj: T, path?: any) {
if (OP.toString.call(obj) === types.array) {
this.overrideArrayProto(obj, path);
@@ -55,7 +55,7 @@ export class JsonOb<T> {
set: function (newVal) {
//cc.log(newVal);
if (oldVal !== newVal) {
if (OP.toString.call(newVal) === '[object Object]') {
if (OP.toString.call(newVal) === types.obj) {
self.observe(newVal, pathArray);
}
@@ -67,11 +67,11 @@ export class JsonOb<T> {
})
// @ts-ignore
if (OP.toString.call(obj[key]) === types.obj || OP.toString.call(obj[key]) === types.array) {
// @ts-ignore
this.observe(obj[key], pathArray);
const o = obj[key];
if (OP.toString.call(o) === types.obj || OP.toString.call(o) === types.array) {
this.observe(o, pathArray);
}
}, this)
}, this);
}
/**
@@ -92,9 +92,9 @@ export class JsonOb<T> {
Object.defineProperty(overrideProto, method, {
value: function () {
var oldVal = this.slice();
//调用原始原型上的方法
// 调用原始原型上的方法
result = originalProto[method].apply(this, arguments);
//继续监听新数组
// 继续监听新数组
self.observe(this, path);
self._callback(this, oldVal, path);
return result;

View File

@@ -1,4 +1,4 @@
import { Component, log, _decorator } from 'cc';
import { _decorator, Component, log } from 'cc';
import { DEBUG } from 'cc/env';
import { VM } from './ViewModel';
import { VMEnv } from './VMEnv';

View File

@@ -1,4 +1,4 @@
import { _decorator, CCString, error } from 'cc';
import { _decorator, CCString, error, Node } from 'cc';
import { StringFormatFunction } from './StringFormat';
import { VMBase } from './VMBase';
import { VMEnv } from './VMEnv';
@@ -22,6 +22,22 @@ const LABEL_TYPE = {
@menu('OopsFramework/Mvvm/VM-Label (标签)')
@help('https://gitee.com/dgflash/oops-framework/wikis/pages?sort_id=12037641&doc_id=2873565')
export default class VMLabel extends VMBase {
/**
* 动态绑定组件
* @param node 目标节点
* @param watchPath 监听数据路径
*/
static bind(node: Node, watchPath: string | string[]) {
let label = node.addComponent(VMLabel);
if (watchPath instanceof Array) {
label.templateMode = true;
label.watchPathArr = watchPath;
}
else {
label.watchPath = watchPath;
}
}
@property({
tooltip: '是否启用模板代码,只能在运行时之前设置,\n将会动态解析模板语法 {{0}},并且自动设置监听的路径'
})

View File

@@ -21,8 +21,8 @@ export default class VMParent extends GameComponent {
/** 需要绑定的私有数据 */
protected data: any = {};
/**VM 管理 */
public VM = VM;
/** VM 管理 */
VM = VM;
/**
* [注意]不能直接覆盖此方法,如果需要覆盖。
@@ -46,7 +46,7 @@ export default class VMParent extends GameComponent {
// console.group();
for (let i = 0; i < comps.length; i++) {
const comp = comps[i];
this.replaceVMPath(comp, this.tag)
this.replaceVMPath(comp, this.tag);
}
// console.groupEnd()
}

View File

@@ -27,7 +27,7 @@ export default class VMProgress extends VMCustom {
})
protected watchPathArr: string[] = ['[min]', '[max]'];
public templateMode: boolean = true;
templateMode: boolean = true;
@property({
visible: function () {

View File

@@ -61,12 +61,12 @@ class ViewModel<T> {
if (this.emitToRootPath) director.emit(VM_EMIT_HEAD + this._tag, n, o, path); // 通知主路径
if (path.length >= 2) {
for (let i = 0; i < path.length - 1; i++) {
const e = path[i];
//log('中端路径');
}
}
// if (path.length >= 2) {
// for (let i = 0; i < path.length - 1; i++) {
// const e = path[i];
// log('中端路径', e);
// }
// }
}
}