@cc-component/cc-ex-component 1.9.3 → 1.9.5
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.
- package/assets/core/ViewModel.ts +62 -15
- package/package.json +1 -1
package/assets/core/ViewModel.ts
CHANGED
|
@@ -64,8 +64,10 @@ interface ICollectViewEvents {
|
|
|
64
64
|
|
|
65
65
|
// 全局存储绑定关系: { className -> { uiProp: dataPath } }
|
|
66
66
|
const bindingMap = new Map<any, Map<string, IBindingData>>();
|
|
67
|
+
const bindingMapFunc = new Map<any, Map<string, string>>();
|
|
68
|
+
|
|
67
69
|
const baseData = 'viewModel'
|
|
68
|
-
const skip = "
|
|
70
|
+
const skip = "func"
|
|
69
71
|
export const refMap: Map<any, string[]> = new Map();
|
|
70
72
|
|
|
71
73
|
// const comType = {
|
|
@@ -111,7 +113,9 @@ export function BindViewModel<T extends new () => any>(constructor: T) {
|
|
|
111
113
|
value.type = classname
|
|
112
114
|
// console.log('属性绑定', propertyKey, value.type, com, this)
|
|
113
115
|
// 记录绑定关系到实例
|
|
114
|
-
this._bindings.push({ uiProp: value.propertyKey, dataPath: value.dataPath });
|
|
116
|
+
//this._bindings.push({ uiProp: value.propertyKey, dataPath: value.dataPath });
|
|
117
|
+
this._bindings[value.dataPath] = value.propertyKey;
|
|
118
|
+
|
|
115
119
|
// 初始化 UI
|
|
116
120
|
const value_data = this._getNestedValue(this, value.dataPath);
|
|
117
121
|
const data = map.get(propertyKey)
|
|
@@ -154,6 +158,15 @@ export function Bind<T extends Component>(path?: string, param?: IBindingDataEve
|
|
|
154
158
|
};
|
|
155
159
|
}
|
|
156
160
|
|
|
161
|
+
export function BindFunc<T extends Component>(path?: string, param?: IBindingDataEvents) {
|
|
162
|
+
return function (target: any, propertyKey: string) {
|
|
163
|
+
const className = target.constructor.prototype
|
|
164
|
+
const funcName = path ? path : propertyKey
|
|
165
|
+
if (!bindingMapFunc.has(className)) { bindingMapFunc.set(className, new Map()); }
|
|
166
|
+
bindingMapFunc.get(className)!.set(propertyKey, funcName);
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
157
170
|
|
|
158
171
|
export function BindTable<T extends Component>(dataPath: string, param?: ITableViewEvents) {
|
|
159
172
|
dataPath = baseData + '.' + dataPath;
|
|
@@ -197,7 +210,9 @@ export function BindCollect<T extends Component>(dataPath: string, param?: IColl
|
|
|
197
210
|
export function ViewModel<T extends new (...args: any[]) => Component>(Base: T) {
|
|
198
211
|
return class extends Base {
|
|
199
212
|
// 绑定关系存储
|
|
200
|
-
_bindings: { uiProp: string; dataPath: string }[] = [];
|
|
213
|
+
//_bindings: { uiProp: string; dataPath: string }[] = [];
|
|
214
|
+
_bindings: Record<string, string> = {}; // key: dataPath, value: uiProp
|
|
215
|
+
|
|
201
216
|
viewModel: any
|
|
202
217
|
touch_start = 'CALL_TOUCH_START'
|
|
203
218
|
touch_move = 'CALL_TOUCH_MOVE'
|
|
@@ -244,12 +259,19 @@ export function ViewModel<T extends new (...args: any[]) => Component>(Base: T)
|
|
|
244
259
|
return value;
|
|
245
260
|
},
|
|
246
261
|
set(target, key: string, value: any) {
|
|
247
|
-
|
|
248
|
-
|
|
262
|
+
//绑定的方法
|
|
263
|
+
const funcName = bindingMapFunc.get(self.className)?.get(key)
|
|
264
|
+
if (funcName) {
|
|
249
265
|
target[key] = value;
|
|
250
266
|
self.refreshData(key, value)
|
|
251
267
|
return true;
|
|
252
268
|
}
|
|
269
|
+
// ✅ 跳过 $ 开头的属性:不触发响应式更新
|
|
270
|
+
else if (key.startsWith(skip)) {
|
|
271
|
+
target[key] = value;
|
|
272
|
+
// self.refreshData(key, value)
|
|
273
|
+
return true;
|
|
274
|
+
}
|
|
253
275
|
else if (value?.skip) {
|
|
254
276
|
target[key] = value.value;
|
|
255
277
|
return true;
|
|
@@ -268,25 +290,41 @@ export function ViewModel<T extends new (...args: any[]) => Component>(Base: T)
|
|
|
268
290
|
}
|
|
269
291
|
|
|
270
292
|
// 数据变化回调(可被子类 override)
|
|
293
|
+
// _onDataChange(fullPath: string, newValue: any) {
|
|
294
|
+
// if (!this._bindings) return;
|
|
295
|
+
// this._bindings.forEach(({ uiProp, dataPath }) => {
|
|
296
|
+
// if (fullPath === dataPath) {
|
|
297
|
+
// const com = this[uiProp]
|
|
298
|
+
// if (com) {
|
|
299
|
+
// const classname = this.constructor.prototype
|
|
300
|
+
// const data = bindingMap.get(classname).get(uiProp)
|
|
301
|
+
// const value = this._getNestedValue(this, data.dataPath);
|
|
302
|
+
// updateStatus(data, com, value, this)
|
|
303
|
+
// }
|
|
304
|
+
// }
|
|
305
|
+
// });
|
|
306
|
+
// }
|
|
307
|
+
|
|
271
308
|
_onDataChange(fullPath: string, newValue: any) {
|
|
272
309
|
if (!this._bindings) return;
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
310
|
+
const uiProp = this._bindings[fullPath];
|
|
311
|
+
if (uiProp !== undefined) {
|
|
312
|
+
const com = this[uiProp];
|
|
313
|
+
if (com) {
|
|
314
|
+
const classname = this.constructor.prototype;
|
|
315
|
+
const data = bindingMap.get(classname)?.get(uiProp);
|
|
316
|
+
if (data) {
|
|
279
317
|
const value = this._getNestedValue(this, data.dataPath);
|
|
280
|
-
updateStatus(data, com, value, this)
|
|
318
|
+
updateStatus(data, com, value, this);
|
|
281
319
|
}
|
|
282
320
|
}
|
|
283
|
-
}
|
|
321
|
+
}
|
|
284
322
|
}
|
|
285
323
|
|
|
286
324
|
/** 解除当前组件的所有数据绑定 */
|
|
287
325
|
public unbindViewModel(): void {
|
|
288
326
|
// 1. 清空绑定关系(停止 _onDataChange 触发 UI 更新)
|
|
289
|
-
this._bindings =
|
|
327
|
+
this._bindings = null;
|
|
290
328
|
// 2. 替换 data 为普通对象(破坏 Proxy 响应式)
|
|
291
329
|
this.viewModel = null
|
|
292
330
|
this.managerData()
|
|
@@ -294,6 +332,7 @@ export function ViewModel<T extends new (...args: any[]) => Component>(Base: T)
|
|
|
294
332
|
|
|
295
333
|
protected onLoad(): void {
|
|
296
334
|
super.onLoad()
|
|
335
|
+
this.bindFunc()
|
|
297
336
|
this.onTouch()
|
|
298
337
|
this.managerData()
|
|
299
338
|
}
|
|
@@ -317,8 +356,16 @@ export function ViewModel<T extends new (...args: any[]) => Component>(Base: T)
|
|
|
317
356
|
// this.offTouch()
|
|
318
357
|
}
|
|
319
358
|
|
|
320
|
-
|
|
359
|
+
bindFunc() {
|
|
360
|
+
}
|
|
321
361
|
|
|
362
|
+
private refreshData(key: string, value: any) {
|
|
363
|
+
const funcName = bindingMapFunc.get(this.className)?.get(key)
|
|
364
|
+
if (this[funcName]) {
|
|
365
|
+
this[funcName]?.(value)
|
|
366
|
+
} else {
|
|
367
|
+
//console.error('没有找到方法:', funcName, this.node.name)
|
|
368
|
+
}
|
|
322
369
|
}
|
|
323
370
|
|
|
324
371
|
resetButtonScale(btn: Button) {
|