@cc-component/cc-ex-component 1.9.9 → 2.0.2

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.
@@ -17,12 +17,14 @@ export class BaseReference extends Component {
17
17
  refList.forEach((value) => { this.defineProperty(value); });
18
18
  }
19
19
 
20
- onLoadFinish() {
20
+ private onLoadFinish() {
21
21
  if (this.vmParams) {
22
22
  this.viewModel?.refreshUI(this.vmParams)
23
23
  this.vmParams = null
24
24
  }
25
25
  }
26
+ onLoadFinished() {
27
+ }
26
28
 
27
29
  //#endregion
28
30
  defineProperty(propertyKey: string) {
@@ -20,6 +20,8 @@ import { YXPagelayout } from '../lib/collectView/lib_collect/yx-page-layout';
20
20
  import { SpriteFrame } from 'cc';
21
21
  import { tween } from 'cc';
22
22
  import { v3 } from 'cc';
23
+ import { BaseReference } from '../core/BaseReference';
24
+
23
25
 
24
26
  interface IBindingData extends ITableViewEvents, IBindingDataEvents {
25
27
  dataPath: string;
@@ -69,6 +71,7 @@ interface ICollectViewEvents {
69
71
  // 全局存储绑定关系: { className -> { uiProp: dataPath } }
70
72
  const bindingMap = new Map<any, Map<string, IBindingData>>();
71
73
  const bindingMapFunc = new Map<any, Map<string, string>>();
74
+ const bindingMapView = new Map<any, { key: string, manager: any }>();
72
75
 
73
76
  const baseData = 'viewModel'
74
77
  const skip = "func"
@@ -105,6 +108,7 @@ export function BindViewModel<T extends new () => any>(constructor: T) {
105
108
  const instance = new constructor();
106
109
  this[baseData] = this._makeReactive(instance, baseData);
107
110
 
111
+ const self = this
108
112
  const map = bindingMap.get(className)
109
113
  for (const [propertyKey, value] of map) {
110
114
  //属性绑定
@@ -115,7 +119,15 @@ export function BindViewModel<T extends new () => any>(constructor: T) {
115
119
  const com = this[propertyKey];
116
120
  const classname = js.getClassName(com)
117
121
  value.type = classname
118
- // console.log('属性绑定', propertyKey, value.type, com, this)
122
+ //绑定自定义的继承BaseReference 的类的viewModel
123
+ if (com instanceof BaseReference) {
124
+ const onLoadFinished = com.onLoadFinished;
125
+ com.onLoadFinished = function () {
126
+ self.viewModel[propertyKey] = com.viewModel
127
+ onLoadFinished.call(com);
128
+ }
129
+ }
130
+ //console.log('属性绑定', propertyKey, value.type, com, this)
119
131
  // 记录绑定关系到实例
120
132
  //this._bindings.push({ uiProp: value.propertyKey, dataPath: value.dataPath });
121
133
  this._bindings[value.dataPath] = value.propertyKey;
@@ -162,7 +174,7 @@ export function Bind<T extends Component>(path?: string, param?: IBindingDataEve
162
174
  };
163
175
  }
164
176
 
165
- export function BindFunc<T extends Component>(path?: string, param?: IBindingDataEvents) {
177
+ export function BindFunc(path?: string) {
166
178
  return function (target: any, propertyKey: string) {
167
179
  const className = target.constructor.prototype
168
180
  const funcName = path ? path : propertyKey
@@ -170,7 +182,17 @@ export function BindFunc<T extends Component>(path?: string, param?: IBindingDat
170
182
  bindingMapFunc.get(className)!.set(propertyKey, funcName);
171
183
  };
172
184
  }
185
+ /**绑定自定义viewModel */
186
+ export function BindData(viewName?: string) {
187
+ return function (target: any, propertyKey: string) {
188
+ const viewClassName = viewName ? viewName : propertyKey
189
+ const className = target.constructor
190
+ if (!bindingMapView.has(viewClassName)) { bindingMapView.set(viewClassName, { key: '', manager: null }); }
191
+ bindingMapView.get(viewClassName)!.key = propertyKey
192
+ bindingMapView.get(viewClassName)!.manager = className
173
193
 
194
+ };
195
+ }
174
196
 
175
197
  export function BindTable<T extends Component>(dataPath: string, param?: ITableViewEvents) {
176
198
  dataPath = baseData + '.' + dataPath;
@@ -222,20 +244,21 @@ export function ViewModel<T extends new (...args: any[]) => Component>(Base: T)
222
244
  _bindings: Record<string, string> = {}; // key: dataPath, value: uiProp
223
245
 
224
246
  viewModel: any
225
- touch_start = 'CALL_TOUCH_START'
226
- touch_move = 'CALL_TOUCH_MOVE'
227
- touch_end = 'CALL_TOUCH_END'
228
- touch_cancel = 'CALL_TOUCH_CANCEL'
229
-
230
- touch_start_scale = 'touch_start_scale'
231
- touch_end_scale = 'touch_end_scale'
232
- touch_cancel_scale = 'touch_cancel_scale'
233
-
234
- touch_click = 'CALL_CLICK'
235
- touch_slide = 'CALL_SLIDE_EVENT'
236
- touch_toggle = 'CALL_TOGGLE_EVENT'
237
- touch_editbox = 'CALL_EDITBOX_EVENT'
238
- className: any
247
+ private touch_start = 'CALL_TOUCH_START'
248
+ private touch_move = 'CALL_TOUCH_MOVE'
249
+ private touch_end = 'CALL_TOUCH_END'
250
+ private touch_cancel = 'CALL_TOUCH_CANCEL'
251
+
252
+ private touch_start_scale = 'touch_start_scale'
253
+ private touch_end_scale = 'touch_end_scale'
254
+ private touch_cancel_scale = 'touch_cancel_scale'
255
+
256
+ private touch_click = 'CALL_CLICK'
257
+ private touch_slide = 'CALL_SLIDE_EVENT'
258
+ private touch_toggle = 'CALL_TOGGLE_EVENT'
259
+ private touch_editbox = 'CALL_EDITBOX_EVENT'
260
+ /**是对象 */
261
+ private className: any
239
262
 
240
263
  /**ui配置 */
241
264
  config: {
@@ -343,13 +366,13 @@ export function ViewModel<T extends new (...args: any[]) => Component>(Base: T)
343
366
  protected onLoad(): void {
344
367
  super.onLoad()
345
368
  this.isOnLoad = true;
346
- this.bindFunc()
347
369
  this.onTouch()
348
370
  this.managerData()
349
371
  }
350
372
  // 在 withDataBinding 返回的 class 中
351
373
  protected onDestroy(): void {
352
374
  this.unbindViewModel();
375
+ this.bindData(false)
353
376
  super.onDestroy?.();
354
377
  }
355
378
 
@@ -357,6 +380,7 @@ export function ViewModel<T extends new (...args: any[]) => Component>(Base: T)
357
380
 
358
381
  onLoadFinished() {
359
382
  this.refreshFuncName()
383
+ this.bindData(true)
360
384
  }
361
385
 
362
386
  managerData() {
@@ -372,7 +396,14 @@ export function ViewModel<T extends new (...args: any[]) => Component>(Base: T)
372
396
  // this.offTouch()
373
397
  }
374
398
 
375
- bindFunc() {
399
+
400
+ bindData(isBind: boolean) {
401
+ const name = js.getClassName(this)
402
+ const managerData = bindingMapView.get(name)
403
+ if (managerData) {
404
+ const manager: any = ExComponentModule.EmitBindData(managerData.manager)
405
+ manager[managerData.key] = isBind ? this.viewModel : null
406
+ }
376
407
  }
377
408
 
378
409
  private refreshData(key: string, value: any) {
@@ -597,10 +628,12 @@ export function ViewModel<T extends new (...args: any[]) => Component>(Base: T)
597
628
  };
598
629
  }
599
630
 
631
+
632
+
600
633
  function updateStatus(data: IBindingData, com: Component, value: any, self: any) {
601
634
  const sx = data.dataPath.split('.').pop();
602
635
  const tempData = { value: value, skip: false }
603
- //console.log("updateStatus", data.propertyKey)
636
+ // console.log("updateStatus", data.propertyKey, data.type, com)
604
637
  switch (data.type) {
605
638
  case 'cc.Label':
606
639
  if (data.reset) {
@@ -826,11 +859,13 @@ function updateStatus(data: IBindingData, com: Component, value: any, self: any)
826
859
  case 'cc.Node':
827
860
  break;
828
861
  default: {
829
- // if (com) console.error(`请检查类型: ${data.type}`);
862
+ if (com) {
863
+ // console.error(`请检查类型: ${data.type}---${data.dataPath}--${data.propertyKey}`);
864
+ }
830
865
  break
831
866
  }
832
- }
833
867
 
868
+ }
834
869
  }
835
870
  // assets/cc-ex-component/core/ViewModel.ts
836
871
 
@@ -20,7 +20,7 @@ export class ExComponentModule {
20
20
  paramSpinePlay: { play: (param: { spine: sp.Skeleton, value: { path: string, bundle: string, param?: ISpinePlayData[] } }) => void }
21
21
 
22
22
 
23
- paramUIWindow: { onLoad: (config: any, viewModel: any) => void }
23
+ paramUIWindow: { onLoad: (config: any, viewModel: any) => void, bindData: (className: any) => any }
24
24
  paramClose: { close: (openParam: any, closeParam: any) => void }
25
25
 
26
26
  ResetBaseWindow: string = "BaseWindow";
@@ -46,7 +46,7 @@ export class ExComponentModule {
46
46
  if (!isGet) ExComponentModule.Ins.ResetBaseWindow = name
47
47
  return ExComponentModule.Ins.ResetBaseWindow
48
48
  }
49
- static OnVMManager(param: { onLoad: (config: any, viewModel: any) => void }) {
49
+ static OnVMManager(param: { onLoad: (config: any, viewModel: any) => void, bindData: (className: any) => any }) {
50
50
  ExComponentModule.Ins.paramUIWindow = param
51
51
  }
52
52
 
@@ -88,6 +88,10 @@ export class ExComponentModule {
88
88
  return ExComponentModule.Ins.paramUIWindow?.onLoad?.(param, viewModel)
89
89
  }
90
90
 
91
+ static EmitBindData(className: any) {
92
+ return ExComponentModule.Ins.paramUIWindow?.bindData?.(className)
93
+ }
94
+
91
95
  static EmitCloseWindow(openParam: any, closeParam: any) {
92
96
  return ExComponentModule.Ins.paramClose?.close?.(openParam, closeParam)
93
97
  }
@@ -35,7 +35,8 @@ declare global {
35
35
  /**重置基础窗口 */
36
36
  function ResetBaseWindow(name?: string, isGet?: boolean);
37
37
  /**监听UI onLoad 给模块manager 赋值 */
38
- function OnVMManager(param: { onLoad: (config: IConfig, viewModel: any) => void });
38
+ function OnVMManager(param: { onLoad: (config: IConfig, viewModel: any) => void, bindData: (className: string) => any });
39
+
39
40
  /**组件:内部使用方法 */
40
41
  function Emit(btn: Button);
41
42
  function EmitAnim();
@@ -45,6 +46,7 @@ declare global {
45
46
  function EmitLoadSpine(param: any): Promise<sp.SkeletonData>;
46
47
  function EmitGetSpine(param: any): sp.SkeletonData;
47
48
  function EmitUIWindow(param: IConfig, viewModel: any);
49
+ function EmitBindData(className: any);
48
50
  function EmitCloseWindow(openParam: any, closeParam: any);
49
51
  function EmitSpinePlay(param: { spine: sp.Skeleton, value: { path: string, bundle: string, param?: ISpinePlayData[] } });
50
52
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cc-component/cc-ex-component",
3
- "version": "1.9.9",
3
+ "version": "2.0.2",
4
4
  "engine": ">=3.8.6",
5
5
  "description": "系统组件添加常用扩展方法",
6
6
  "main": "index.ts",