From 970990ea2b31ed00907aeda8c454513071230b67 Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Thu, 29 Feb 2024 01:07:57 +0400 Subject: [PATCH] Improve data variable listeners --- src/dataSources/index.ts | 11 ++++++++--- .../view/ComponentDataVariableView.ts | 15 ++++++++++++++- test/specs/dataSources/index.ts | 10 ++++++++-- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/dataSources/index.ts b/src/dataSources/index.ts index f84757a82..f8645b58a 100644 --- a/src/dataSources/index.ts +++ b/src/dataSources/index.ts @@ -42,12 +42,17 @@ export default class DataSourceManager extends ItemManagerModule { initialize(opt = {}) { super.initialize(opt); + this.listenToData(); + } + + listenToData() { const { model, em } = this; const { path } = model.attributes; - this.listenTo(em, `${DataSourcesEvents.path}:${path}`, () => this.postRender()); + const normPath = stringToPath(path || '').join('.'); + const [ds, dr] = em.DataSources.fromPath(path); + + if (ds) { + this.listenTo(ds.records, 'add remove reset', this.postRender); + dr && this.listenTo(dr, 'change', this.postRender); + } + + this.listenTo(em, `${DataSourcesEvents.path}:${normPath}`, this.postRender); } postRender() { diff --git a/test/specs/dataSources/index.ts b/test/specs/dataSources/index.ts index 5f789d4c5..58c22eee4 100644 --- a/test/specs/dataSources/index.ts +++ b/test/specs/dataSources/index.ts @@ -92,12 +92,18 @@ describe('DataSourceManager', () => { const cmpVar = cmpRoot.append({ type: 'data-variable', value: 'default', - path: 'ds1.id4.name', + path: 'ds1[id4]name', })[0]; - ds.addRecord({ id: 'id4', name: 'Name4' }); + const newRecord = ds.addRecord({ id: 'id4', name: 'Name4' }); expect(cmpVar.getEl()?.innerHTML).toBe('Name4'); + newRecord.set({ name: 'up' }); + expect(cmpVar.getEl()?.innerHTML).toBe('up'); }); + test('component is properly updating on data source add', () => {}); + + test('component is properly updating on data source reset', () => {}); + test('component is properly updating on record change', () => {}); test('component is properly updating on record remove', () => {});