diff --git a/assets/libs/model-view/JsonOb.ts b/assets/libs/model-view/JsonOb.ts index b1ab1a3..d902ffd 100644 --- a/assets/libs/model-view/JsonOb.ts +++ b/assets/libs/model-view/JsonOb.ts @@ -30,7 +30,7 @@ export class JsonOb { private _callback; - /**对象属性劫持 */ + /** 对象属性劫持 */ private observe(obj: T, path?: any) { if (OP.toString.call(obj) === types.array) { this.overrideArrayProto(obj, path); @@ -55,7 +55,7 @@ export class JsonOb { 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 { }) // @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 { 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; diff --git a/assets/libs/model-view/VMBase.ts b/assets/libs/model-view/VMBase.ts index da623a3..b1ba08d 100644 --- a/assets/libs/model-view/VMBase.ts +++ b/assets/libs/model-view/VMBase.ts @@ -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'; diff --git a/assets/libs/model-view/VMLabel.ts b/assets/libs/model-view/VMLabel.ts index 6b4da4e..9f5c1c6 100644 --- a/assets/libs/model-view/VMLabel.ts +++ b/assets/libs/model-view/VMLabel.ts @@ -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}},并且自动设置监听的路径' }) diff --git a/assets/libs/model-view/VMParent.ts b/assets/libs/model-view/VMParent.ts index f176b49..e3c154d 100644 --- a/assets/libs/model-view/VMParent.ts +++ b/assets/libs/model-view/VMParent.ts @@ -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() } diff --git a/assets/libs/model-view/VMProgress.ts b/assets/libs/model-view/VMProgress.ts index d41a043..192477a 100644 --- a/assets/libs/model-view/VMProgress.ts +++ b/assets/libs/model-view/VMProgress.ts @@ -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 () { diff --git a/assets/libs/model-view/ViewModel.ts b/assets/libs/model-view/ViewModel.ts index 1d1af9c..ea77da9 100644 --- a/assets/libs/model-view/ViewModel.ts +++ b/assets/libs/model-view/ViewModel.ts @@ -61,12 +61,12 @@ class ViewModel { 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); + // } + // } } }