@cc-component/cc-ex-component 1.9.4 → 1.9.6

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.
@@ -64,6 +64,8 @@ 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
70
  const skip = "func"
69
71
  export const refMap: Map<any, string[]> = new Map();
@@ -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)
@@ -120,7 +124,7 @@ export function BindViewModel<T extends new () => any>(constructor: T) {
120
124
 
121
125
  this.onLoadFinish();
122
126
  onLoad.call(this);
123
-
127
+ this.onLoadFinished();
124
128
  };
125
129
  };
126
130
 
@@ -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'
@@ -226,6 +241,8 @@ export function ViewModel<T extends new (...args: any[]) => Component>(Base: T)
226
241
  /**是否记录-不记录可重复打开窗口 */
227
242
  is_record?: boolean;
228
243
  };
244
+ isOnLoad: boolean = false
245
+ funcMap: Map<string, any> = new Map()
229
246
  // 创建响应式数据
230
247
  _makeReactive<T extends object>(obj: T, pathPrefix: string): T {
231
248
  const self = this as any;
@@ -244,12 +261,19 @@ export function ViewModel<T extends new (...args: any[]) => Component>(Base: T)
244
261
  return value;
245
262
  },
246
263
  set(target, key: string, value: any) {
247
- // ✅ 跳过 $ 开头的属性:不触发响应式更新
248
- if (key.startsWith(skip)) {
264
+ //绑定的方法
265
+ const funcName = bindingMapFunc.get(self.className)?.get(key)
266
+ if (funcName) {
249
267
  target[key] = value;
250
268
  self.refreshData(key, value)
251
269
  return true;
252
270
  }
271
+ // ✅ 跳过 $ 开头的属性:不触发响应式更新
272
+ else if (key.startsWith(skip)) {
273
+ target[key] = value;
274
+ // self.refreshData(key, value)
275
+ return true;
276
+ }
253
277
  else if (value?.skip) {
254
278
  target[key] = value.value;
255
279
  return true;
@@ -268,25 +292,41 @@ export function ViewModel<T extends new (...args: any[]) => Component>(Base: T)
268
292
  }
269
293
 
270
294
  // 数据变化回调(可被子类 override)
295
+ // _onDataChange(fullPath: string, newValue: any) {
296
+ // if (!this._bindings) return;
297
+ // this._bindings.forEach(({ uiProp, dataPath }) => {
298
+ // if (fullPath === dataPath) {
299
+ // const com = this[uiProp]
300
+ // if (com) {
301
+ // const classname = this.constructor.prototype
302
+ // const data = bindingMap.get(classname).get(uiProp)
303
+ // const value = this._getNestedValue(this, data.dataPath);
304
+ // updateStatus(data, com, value, this)
305
+ // }
306
+ // }
307
+ // });
308
+ // }
309
+
271
310
  _onDataChange(fullPath: string, newValue: any) {
272
311
  if (!this._bindings) return;
273
- this._bindings.forEach(({ uiProp, dataPath }) => {
274
- if (fullPath === dataPath) {
275
- const com = this[uiProp]
276
- if (com) {
277
- const classname = this.constructor.prototype
278
- const data = bindingMap.get(classname).get(uiProp)
312
+ const uiProp = this._bindings[fullPath];
313
+ if (uiProp !== undefined) {
314
+ const com = this[uiProp];
315
+ if (com) {
316
+ const classname = this.constructor.prototype;
317
+ const data = bindingMap.get(classname)?.get(uiProp);
318
+ if (data) {
279
319
  const value = this._getNestedValue(this, data.dataPath);
280
- updateStatus(data, com, value, this)
320
+ updateStatus(data, com, value, this);
281
321
  }
282
322
  }
283
- });
323
+ }
284
324
  }
285
325
 
286
326
  /** 解除当前组件的所有数据绑定 */
287
327
  public unbindViewModel(): void {
288
328
  // 1. 清空绑定关系(停止 _onDataChange 触发 UI 更新)
289
- this._bindings = [];
329
+ this._bindings = null;
290
330
  // 2. 替换 data 为普通对象(破坏 Proxy 响应式)
291
331
  this.viewModel = null
292
332
  this.managerData()
@@ -294,6 +334,8 @@ export function ViewModel<T extends new (...args: any[]) => Component>(Base: T)
294
334
 
295
335
  protected onLoad(): void {
296
336
  super.onLoad()
337
+ this.isOnLoad = true;
338
+ this.bindFunc()
297
339
  this.onTouch()
298
340
  this.managerData()
299
341
  }
@@ -304,6 +346,11 @@ export function ViewModel<T extends new (...args: any[]) => Component>(Base: T)
304
346
  }
305
347
 
306
348
 
349
+
350
+ onLoadFinished() {
351
+ this.refreshFuncName()
352
+ }
353
+
307
354
  managerData() {
308
355
  ExComponentModule.EmitUIWindow(this.config, this.viewModel)
309
356
  }
@@ -317,15 +364,22 @@ export function ViewModel<T extends new (...args: any[]) => Component>(Base: T)
317
364
  // this.offTouch()
318
365
  }
319
366
 
367
+ bindFunc() {
368
+ }
369
+
320
370
  private refreshData(key: string, value: any) {
321
- const funcName = `${key}`
322
- if (this[funcName]) {
371
+ const funcName = bindingMapFunc.get(this.className)?.get(key)
372
+ if (this.isOnLoad) {
323
373
  this[funcName]?.(value)
324
374
  } else {
325
- console.error('没有找到方法:', funcName, this.node.name)
375
+ this.funcMap.set(funcName, value)
326
376
  }
327
377
  }
328
378
 
379
+ private refreshFuncName() {
380
+ this.funcMap.forEach((value, key) => { this[key]?.(value) })
381
+ }
382
+
329
383
  resetButtonScale(btn: Button) {
330
384
  if (btn.transition === Button.Transition.SCALE && ExComponentModule.EmitAnim()) {
331
385
  if (btn[this.touch_start_scale]) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cc-component/cc-ex-component",
3
- "version": "1.9.4",
3
+ "version": "1.9.6",
4
4
  "engine": ">=3.8.6",
5
5
  "description": "系统组件添加常用扩展方法",
6
6
  "main": "index.ts",