diff --git a/src/data_sources/model/ComponentDataVariable.ts b/src/data_sources/model/ComponentDataVariable.ts index 32533a2e3..6f740ed4b 100644 --- a/src/data_sources/model/ComponentDataVariable.ts +++ b/src/data_sources/model/ComponentDataVariable.ts @@ -1,6 +1,6 @@ import Component from '../../dom_components/model/Component'; import { ToHTMLOptions } from '../../dom_components/model/types'; -import { stringToPath, toLowerCase } from '../../utils/mixins'; +import { toLowerCase } from '../../utils/mixins'; import { DataVariableType } from './DataVariable'; export default class ComponentDataVariable extends Component { @@ -14,10 +14,11 @@ export default class ComponentDataVariable extends Component { }; } - getInnerHTML(opts: ToHTMLOptions & { keepVariables?: boolean } = {}) { + getInnerHTML(opts: ToHTMLOptions) { const { path, value } = this.attributes; + const val = this.em.DataSources.getValue(path, value); - return opts.keepVariables ? path : this.em.DataSources.getValue(path, value); + return val; } static isComponent(el: HTMLElement) { diff --git a/src/data_sources/model/DataVariable.ts b/src/data_sources/model/DataVariable.ts index 4fa2b9add..4b8569c6e 100644 --- a/src/data_sources/model/DataVariable.ts +++ b/src/data_sources/model/DataVariable.ts @@ -36,10 +36,9 @@ export default class DataVariable extends Model { } getDataValue() { - const { path } = this.attributes; - const [dsId, drId, key] = stringToPath(path); - const ds = this?.em?.DataSources.get(dsId); - const dr = ds && ds.getRecord(drId); - return dr?.get(key); + const { path, value } = this.attributes; + const val = this.em?.DataSources?.getValue?.(path, value); + + return val; } } diff --git a/src/domain_abstract/model/StyleableModel.ts b/src/domain_abstract/model/StyleableModel.ts index be07f5841..08bdc48b6 100644 --- a/src/domain_abstract/model/StyleableModel.ts +++ b/src/domain_abstract/model/StyleableModel.ts @@ -148,11 +148,7 @@ export default class StyleableModel extends Model dataListeners.forEach((ls) => this.listenTo(ls.obj, ls.event, () => { - const [dsId, drId, keyPath] = stringToPath(path); - const ds = em?.DataSources.get(dsId); - const dr = ds && ds.records.get(drId); - const newValue = dr && dr.get(keyPath); - + const newValue = dataVar.getDataValue(); this.updateStyleProp(styleProp, newValue); }), ); @@ -185,12 +181,7 @@ export default class StyleableModel extends Model } if (styleValue instanceof StyleDataVariable) { - const [dsId, drId, keyPath] = stringToPath(styleValue.get('path')); - const ds = this.em?.DataSources.get(dsId); - const dr = ds && ds.records.get(drId); - const resolvedValue = dr && dr.get(keyPath); - - resolvedStyle[key] = resolvedValue || styleValue.get('value'); + resolvedStyle[key] = styleValue.getDataValue(); } }); return resolvedStyle; diff --git a/test/specs/data_sources/model/StyleDataVariable.ts b/test/specs/data_sources/model/StyleDataVariable.ts index 330abc361..01ced18ed 100644 --- a/test/specs/data_sources/model/StyleDataVariable.ts +++ b/test/specs/data_sources/model/StyleDataVariable.ts @@ -105,4 +105,41 @@ describe('StyleDataVariable', () => { const style = cmp.getStyle(); expect(style).toHaveProperty('color', 'black'); }); + + test('component initializes and updates with data-variable style for nested object', () => { + const styleDataSource: DataSourceProps = { + id: 'style-data', + records: [ + { + id: 'id1', + nestedObject: { + color: 'red', + }, + }, + ], + }; + dsm.add(styleDataSource); + + const cmp = cmpRoot.append({ + tagName: 'h1', + type: 'text', + content: 'Hello World', + style: { + color: { + type: DataVariableType, + value: 'black', + path: 'style-data.id1.nestedObject.color', + }, + }, + })[0]; + + const style = cmp.getStyle(); + expect(style).toHaveProperty('color', 'red'); + + const ds = dsm.get('style-data'); + ds.getRecord('id1')?.set({ nestedObject: { color: 'blue' } }); + + const updatedStyle = cmp.getStyle(); + expect(updatedStyle).toHaveProperty('color', 'blue'); + }); }); diff --git a/test/specs/data_sources/model/TraitDataVariable.ts b/test/specs/data_sources/model/TraitDataVariable.ts index 32c57fef4..fdbecb485 100644 --- a/test/specs/data_sources/model/TraitDataVariable.ts +++ b/test/specs/data_sources/model/TraitDataVariable.ts @@ -134,6 +134,42 @@ describe('TraitDataVariable', () => { expect(input?.getAttribute('value')).toBe('new-value'); expect(cmp?.getAttributes().value).toBe('new-value'); }); + + test('component initializes data-variable value for nested object', () => { + const inputDataSource: DataSourceProps = { + id: 'nested-input-data', + records: [ + { + id: 'id1', + nestedObject: { + value: 'nested-value', + }, + }, + ], + }; + dsm.add(inputDataSource); + + const cmp = cmpRoot.append({ + tagName: 'input', + traits: [ + 'name', + { + type: 'text', + label: 'Value', + name: 'value', + value: { + type: DataVariableType, + value: 'default', + path: 'nested-input-data.id1.nestedObject.value', + }, + }, + ], + })[0]; + + const input = cmp.getEl(); + expect(input?.getAttribute('value')).toBe('nested-value'); + expect(cmp?.getAttributes().value).toBe('nested-value'); + }); }); describe('checkbox input component', () => {