Browse Source

feat: add setRecords back

pull/6018/head
danstarns 1 year ago
parent
commit
733656f806
  1. 11
      docs/api/datasource.md
  2. 16
      src/data_sources/model/DataSource.ts
  3. 27
      test/specs/data_sources/model/ComponentDataVariable.ts

11
docs/api/datasource.md

@ -106,6 +106,17 @@ If a transformer is provided for the `onRecordDelete` event, it will be applied
Returns **(DataRecord | [undefined][8])** The removed data record, or `undefined` if no record is found with the given ID.
## setRecords
Replaces the existing records in the data source with a new set of records.
If a transformer is provided for the `onRecordAdd` event, it will be applied to each record before adding it.
### Parameters
* `records` **[Array][9]\<DataRecordProps>** An array of data record properties to set.
Returns **[Array][9]\<DataRecord>** An array of the added data records.
[1]: #addrecord
[2]: #getrecord

16
src/data_sources/model/DataSource.ts

@ -177,4 +177,20 @@ export default class DataSource extends Model<DataSourceProps> {
return this.records.remove(id, opts);
}
/**
* Replaces the existing records in the data source with a new set of records.
* If a transformer is provided for the `onRecordAdd` event, it will be applied to each record before adding it.
*
* @param {Array<DataRecordProps>} records - An array of data record properties to set.
* @returns {Array<DataRecord>} An array of the added data records.
* @name setRecords
*/
setRecords(records: Array<DataRecordProps>) {
this.records.reset([], { silent: true });
records.forEach((record) => {
this.records.add(record, { avoidTransformers: true });
});
}
}

27
test/specs/data_sources/model/ComponentDataVariable.ts

@ -124,6 +124,33 @@ describe('ComponentDataVariable', () => {
expect(cmp.getEl()?.innerHTML).toContain('default');
});
test('component updates on data source setRecords', () => {
const dataSource: DataSourceProps = {
id: 'component-setRecords',
records: [{ id: 'id1', name: 'init name' }],
};
dsm.add(dataSource);
const cmp = cmpRoot.append({
tagName: 'div',
type: 'default',
components: [
{
type: DataVariableType,
value: 'default',
path: `${dataSource.id}.id1.name`,
},
],
})[0];
expect(cmp.getEl()?.innerHTML).toContain('init name');
const ds = dsm.get(dataSource.id);
ds.setRecords([{ id: 'id1', name: 'updated name' }]);
expect(cmp.getEl()?.innerHTML).toContain('updated name');
});
test('component updates on record removal', () => {
const dataSource: DataSourceProps = {
id: 'ds4',

Loading…
Cancel
Save