From bc3e65a0970ea5f67a576dbe76f2c641bd25ce37 Mon Sep 17 00:00:00 2001 From: mohamedsalem401 Date: Mon, 6 Jan 2025 12:20:34 +0200 Subject: [PATCH] Refactor watch dynamic datasource --- .../CollectionComponent.ts | 51 ++++++++++++------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/packages/core/src/data_sources/model/collection_component/CollectionComponent.ts b/packages/core/src/data_sources/model/collection_component/CollectionComponent.ts index fe6d64d92..27cac8822 100644 --- a/packages/core/src/data_sources/model/collection_component/CollectionComponent.ts +++ b/packages/core/src/data_sources/model/collection_component/CollectionComponent.ts @@ -35,21 +35,7 @@ export default class CollectionComponent extends Component { super(conditionalCmptDef, opt); if (this.hasDynamicDataSource()) { - const path = this.get(keyCollectionDefinition).config.dataSource?.path; - new DynamicVariableListenerManager({ - em: em, - dataVariable: new DataVariable( - { - type: 'data-variable', - path, - }, - { em }, - ), - updateValueFromDataVariable: () => { - const collectionItems = getCollectionItems(em, collectionDefinition, parentCollectionStateMap, opt); - this.components(collectionItems); - }, - }); + this.watchDataSource(em, collectionDefinition, parentCollectionStateMap, opt); } } @@ -65,14 +51,43 @@ export default class CollectionComponent extends Component { toJSON(opts?: ObjectAny) { const json = super.toJSON(opts) as CollectionComponentDefinition; - const firstChild = this.components().at(0)?.toJSON() || {}; - const keysToRemove = ['attributes?.id', keySymbol, keySymbols, keySymbolOvrd, keyCollectionsStateMap]; - keysToRemove.forEach((key) => delete firstChild[key]); + const firstChild = this.getBlockDefinition(); json[keyCollectionDefinition].block = firstChild; delete json.components; return json; } + + private getBlockDefinition() { + const firstChild = this.components().at(0)?.toJSON() || {}; + const keysToRemove = ['attributes?.id', keySymbol, keySymbols, keySymbolOvrd, keyCollectionsStateMap]; + keysToRemove.forEach((key) => delete firstChild[key]); + return firstChild; + } + + private watchDataSource( + em: EditorModel, + collectionDefinition: CollectionDefinition, + parentCollectionStateMap: CollectionsStateMap, + opt: ComponentOptions, + ) { + const path = this.get(keyCollectionDefinition).config.dataSource?.path; + const dataVariable = new DataVariable( + { + type: DataVariableType, + path, + }, + { em }, + ); + new DynamicVariableListenerManager({ + em: em, + dataVariable, + updateValueFromDataVariable: () => { + const collectionItems = getCollectionItems(em, collectionDefinition, parentCollectionStateMap, opt); + this.components(collectionItems); + }, + }); + } } function getCollectionItems(