mirror of https://github.com/artf/grapesjs.git
10 changed files with 832 additions and 40 deletions
@ -0,0 +1,115 @@ |
|||||
|
<!-- Generated by documentation.js. Update this documentation by updating the source code. --> |
||||
|
|
||||
|
## DataRecord |
||||
|
|
||||
|
The `DataRecord` class represents a single record within a data source. |
||||
|
It extends the base `Model` class and provides additional methods and properties specific to data records. |
||||
|
Each `DataRecord` is associated with a `DataSource` and can trigger events when its properties change. |
||||
|
|
||||
|
### DataRecord API |
||||
|
|
||||
|
* [getPath][1] |
||||
|
* [getPaths][2] |
||||
|
* [set][3] |
||||
|
|
||||
|
### Example of Usage |
||||
|
|
||||
|
```js |
||||
|
const record = new DataRecord({ id: 'record1', name: 'value1' }, { collection: dataRecords }); |
||||
|
const path = record.getPath(); // e.g., 'SOURCE_ID.record1' |
||||
|
record.set('name', 'newValue'); |
||||
|
``` |
||||
|
|
||||
|
### Parameters |
||||
|
|
||||
|
* `props` **DataRecordProps** Properties to initialize the data record. |
||||
|
* `opts` **[Object][4]** Options for initializing the data record. |
||||
|
|
||||
|
## getPath |
||||
|
|
||||
|
Get the path of the record. |
||||
|
The path is a string that represents the location of the record within the data source. |
||||
|
Optionally, include a property name to create a more specific path. |
||||
|
|
||||
|
### Parameters |
||||
|
|
||||
|
* `prop` **[String][5]?** Optional property name to include in the path. |
||||
|
* `opts` **[Object][4]?** Options for path generation. |
||||
|
|
||||
|
* `opts.useIndex` **[Boolean][6]?** Whether to use the index of the record in the path. |
||||
|
|
||||
|
### Examples |
||||
|
|
||||
|
```javascript |
||||
|
const pathRecord = record.getPath(); |
||||
|
// e.g., 'SOURCE_ID.record1' |
||||
|
const pathRecord2 = record.getPath('myProp'); |
||||
|
// e.g., 'SOURCE_ID.record1.myProp' |
||||
|
``` |
||||
|
|
||||
|
Returns **[String][5]** The path of the record. |
||||
|
|
||||
|
## getPaths |
||||
|
|
||||
|
Get both ID-based and index-based paths of the record. |
||||
|
Returns an array containing the paths using both ID and index. |
||||
|
|
||||
|
### Parameters |
||||
|
|
||||
|
* `prop` **[String][5]?** Optional property name to include in the paths. |
||||
|
|
||||
|
### Examples |
||||
|
|
||||
|
```javascript |
||||
|
const paths = record.getPaths(); |
||||
|
// e.g., ['SOURCE_ID.record1', 'SOURCE_ID.0'] |
||||
|
``` |
||||
|
|
||||
|
Returns **[Array][7]<[String][5]>** An array of paths. |
||||
|
|
||||
|
## triggerChange |
||||
|
|
||||
|
Trigger a change event for the record. |
||||
|
Optionally, include a property name to trigger a change event for a specific property. |
||||
|
|
||||
|
### Parameters |
||||
|
|
||||
|
* `prop` **[String][5]?** Optional property name to trigger a change event for a specific property. |
||||
|
|
||||
|
## set |
||||
|
|
||||
|
Set a property on the record, optionally using transformers. |
||||
|
If transformers are defined for the record, they will be applied to the value before setting it. |
||||
|
|
||||
|
### Parameters |
||||
|
|
||||
|
* `attributeName` **([String][5] | [Object][4])** The name of the attribute to set, or an object of key-value pairs. |
||||
|
* `value` **any?** The value to set for the attribute. |
||||
|
* `options` **[Object][4]?** Options to apply when setting the attribute. |
||||
|
|
||||
|
* `options.avoidTransformers` **[Boolean][6]?** If true, transformers will not be applied. |
||||
|
|
||||
|
### Examples |
||||
|
|
||||
|
```javascript |
||||
|
record.set('name', 'newValue'); |
||||
|
// Sets 'name' property to 'newValue' |
||||
|
``` |
||||
|
|
||||
|
Returns **[DataRecord][8]** The instance of the DataRecord. |
||||
|
|
||||
|
[1]: #getpath |
||||
|
|
||||
|
[2]: #getpaths |
||||
|
|
||||
|
[3]: #set |
||||
|
|
||||
|
[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object |
||||
|
|
||||
|
[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String |
||||
|
|
||||
|
[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean |
||||
|
|
||||
|
[7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array |
||||
|
|
||||
|
[8]: #datarecord |
||||
@ -0,0 +1,143 @@ |
|||||
|
<!-- Generated by documentation.js. Update this documentation by updating the source code. --> |
||||
|
|
||||
|
## DataSource |
||||
|
|
||||
|
The `DataSource` class represents a data source within the editor. |
||||
|
It manages a collection of data records and provides methods to interact with them. |
||||
|
The `DataSource` can be extended with transformers to modify records during add, read, and delete operations. |
||||
|
|
||||
|
### DataSource API |
||||
|
|
||||
|
* [addRecord][1] |
||||
|
* [getRecord][2] |
||||
|
* [getRecords][3] |
||||
|
* [removeRecord][4] |
||||
|
|
||||
|
### Example of Usage |
||||
|
|
||||
|
```js |
||||
|
const dataSource = new DataSource({ |
||||
|
records: [ |
||||
|
{ id: 'id1', name: 'value1' }, |
||||
|
{ id: 'id2', name: 'value2' } |
||||
|
], |
||||
|
transformers: { |
||||
|
onRecordAdd: ({ record }) => ({ ...record, added: true }), |
||||
|
} |
||||
|
}, { em: editor }); |
||||
|
|
||||
|
dataSource.addRecord({ id: 'id3', name: 'value3' }); |
||||
|
``` |
||||
|
|
||||
|
### Parameters |
||||
|
|
||||
|
* `props` **DataSourceProps** Properties to initialize the data source. |
||||
|
* `opts` **DataSourceOptions** Options to initialize the data source. |
||||
|
|
||||
|
## id |
||||
|
|
||||
|
DataSource id. |
||||
|
|
||||
|
Type: [string][5] |
||||
|
|
||||
|
## records |
||||
|
|
||||
|
DataSource records. |
||||
|
|
||||
|
Type: (DataRecords | [Array][6]\<DataRecord> | [Array][6]\<DataRecordProps>) |
||||
|
|
||||
|
## transformers |
||||
|
|
||||
|
DataSource validation and transformation factories. |
||||
|
|
||||
|
Type: DataSourceTransformers |
||||
|
|
||||
|
## defaults |
||||
|
|
||||
|
Returns the default properties for the data source. |
||||
|
These include an empty array of records and an empty object of transformers. |
||||
|
|
||||
|
Returns **[Object][7]** The default attributes for the data source. |
||||
|
|
||||
|
## constructor |
||||
|
|
||||
|
Initializes a new instance of the `DataSource` class. |
||||
|
It sets up the transformers and initializes the collection of records. |
||||
|
If the `records` property is not an instance of `DataRecords`, it will be converted into one. |
||||
|
|
||||
|
### Parameters |
||||
|
|
||||
|
* `props` **DataSourceProps** Properties to initialize the data source. |
||||
|
* `opts` **DataSourceOptions** Options to initialize the data source. |
||||
|
|
||||
|
## records |
||||
|
|
||||
|
Retrieves the collection of records associated with this data source. |
||||
|
|
||||
|
Returns **DataRecords** The collection of data records. |
||||
|
|
||||
|
## em |
||||
|
|
||||
|
Retrieves the editor model associated with this data source. |
||||
|
|
||||
|
Returns **EditorModel** The editor model. |
||||
|
|
||||
|
## addRecord |
||||
|
|
||||
|
Adds a new record to the data source. |
||||
|
If a transformer is provided for the `onRecordAdd` event, it will be applied to the record before adding it. |
||||
|
|
||||
|
### Parameters |
||||
|
|
||||
|
* `record` **DataRecordProps** The properties of the record to add. |
||||
|
* `opts` **AddOptions?** Options to apply when adding the record. |
||||
|
|
||||
|
Returns **DataRecord** The added data record. |
||||
|
|
||||
|
## getRecord |
||||
|
|
||||
|
Retrieves a record from the data source by its ID. |
||||
|
If a transformer is provided for the `onRecordRead` event, it will be applied to the record before returning it. |
||||
|
|
||||
|
### Parameters |
||||
|
|
||||
|
* `id` **([string][5] | [number][8])** The ID of the record to retrieve. |
||||
|
|
||||
|
Returns **(DataRecord | [undefined][9])** The data record, or `undefined` if no record is found with the given ID. |
||||
|
|
||||
|
## getRecords |
||||
|
|
||||
|
Retrieves all records from the data source. |
||||
|
Each record is processed with the `getRecord` method to apply any read transformers. |
||||
|
|
||||
|
Returns **[Array][6]<(DataRecord | [undefined][9])>** An array of data records. |
||||
|
|
||||
|
## removeRecord |
||||
|
|
||||
|
Removes a record from the data source by its ID. |
||||
|
If a transformer is provided for the `onRecordDelete` event, it will be applied before the record is removed. |
||||
|
|
||||
|
### Parameters |
||||
|
|
||||
|
* `id` **([string][5] | [number][8])** The ID of the record to remove. |
||||
|
* `opts` **RemoveOptions?** Options to apply when removing the record. |
||||
|
|
||||
|
Returns **(DataRecord | [undefined][9])** The removed data record, or `undefined` if no record is found with the given ID. |
||||
|
|
||||
|
[1]: #addrecord |
||||
|
|
||||
|
[2]: #getrecord |
||||
|
|
||||
|
[3]: #getrecords |
||||
|
|
||||
|
[4]: #removerecord |
||||
|
|
||||
|
[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String |
||||
|
|
||||
|
[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array |
||||
|
|
||||
|
[7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object |
||||
|
|
||||
|
[8]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number |
||||
|
|
||||
|
[9]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/undefined |
||||
Loading…
Reference in new issue