Browse Source

feat: reuse DataVariableListenerManager on ComponentDataVar

feature/data-sources
danstarns 2 years ago
parent
commit
74085e8ef8
  1. 8
      src/data_sources/model/ComponentDataVariable.ts
  2. 10
      src/data_sources/model/DataVariableListenerManager.ts
  3. 36
      src/data_sources/view/ComponentDataVariableView.ts

8
src/data_sources/model/ComponentDataVariable.ts

@ -14,9 +14,13 @@ export default class ComponentDataVariable extends Component {
}; };
} }
getInnerHTML(opts: ToHTMLOptions) { getDataValue() {
const { path, defaultValue } = this.attributes; const { path, defaultValue } = this.attributes;
const val = this.em.DataSources.getValue(path, defaultValue); return this.em.DataSources.getValue(path, defaultValue);
}
getInnerHTML(opts: ToHTMLOptions) {
const val = this.getDataValue();
return val; return val;
} }

10
src/data_sources/model/DataVariableListenerManager.ts

@ -3,19 +3,21 @@ import { stringToPath } from '../../utils/mixins';
import { Model } from '../../common'; import { Model } from '../../common';
import EditorModel from '../../editor/model/Editor'; import EditorModel from '../../editor/model/Editor';
import DataVariable from './DataVariable'; import DataVariable from './DataVariable';
import ComponentView from '../../dom_components/view/ComponentView';
import ComponentDataVariable from './ComponentDataVariable';
export interface DataVariableListenerManagerOptions { export interface DataVariableListenerManagerOptions {
model: Model; model: Model | ComponentView;
em: EditorModel; em: EditorModel;
dataVariable: DataVariable; dataVariable: DataVariable | ComponentDataVariable;
updateValueFromDataVariable: (value: any) => void; updateValueFromDataVariable: (value: any) => void;
} }
export default class DataVariableListenerManager { export default class DataVariableListenerManager {
private dataListeners: DataVariableListener[] = []; private dataListeners: DataVariableListener[] = [];
private em: EditorModel; private em: EditorModel;
private model: Model; private model: Model | ComponentView;
private dataVariable: DataVariable; private dataVariable: DataVariable | ComponentDataVariable;
private updateValueFromDataVariable: (value: any) => void; private updateValueFromDataVariable: (value: any) => void;
constructor(options: DataVariableListenerManagerOptions) { constructor(options: DataVariableListenerManagerOptions) {

36
src/data_sources/view/ComponentDataVariableView.ts

@ -1,38 +1,18 @@
import { DataSourcesEvents, DataVariableListener } from '../../data_sources/types';
import ComponentView from '../../dom_components/view/ComponentView'; import ComponentView from '../../dom_components/view/ComponentView';
import { stringToPath } from '../../utils/mixins';
import ComponentDataVariable from '../model/ComponentDataVariable'; import ComponentDataVariable from '../model/ComponentDataVariable';
import DataVariableListenerManager from '../model/DataVariableListenerManager';
export default class ComponentDataVariableView extends ComponentView<ComponentDataVariable> { export default class ComponentDataVariableView extends ComponentView<ComponentDataVariable> {
dataListeners: DataVariableListener[] = []; dataVariableListener?: DataVariableListenerManager;
initialize(opt = {}) { initialize(opt = {}) {
super.initialize(opt); super.initialize(opt);
this.listenToData(); this.dataVariableListener = new DataVariableListenerManager({
this.listenTo(this.model, 'change:path', this.listenToData); model: this,
} em: this.em!,
dataVariable: this.model,
listenToData() { updateValueFromDataVariable: () => this.postRender(),
const { model, em } = this; });
const { path } = model.attributes;
const normPath = stringToPath(path || '').join('.');
const { DataSources } = em;
const [ds, dr] = DataSources.fromPath(path);
const dataListeners: DataVariableListener[] = [];
const prevListeners = this.dataListeners || [];
prevListeners.forEach((ls) => this.stopListening(ls.obj, ls.event, this.postRender));
ds && dataListeners.push({ obj: ds.records, event: 'add remove reset' });
dr && dataListeners.push({ obj: dr, event: 'change' });
dataListeners.push(
{ obj: model, event: 'change:path change:value' },
{ obj: DataSources.all, event: 'add remove reset' },
{ obj: em, event: `${DataSourcesEvents.path}:${normPath}` },
);
dataListeners.forEach((ls) => this.listenTo(ls.obj, ls.event, this.postRender));
this.dataListeners = dataListeners;
} }
postRender() { postRender() {

Loading…
Cancel
Save