mirror of https://github.com/artf/grapesjs.git
5 changed files with 214 additions and 2 deletions
@ -0,0 +1,159 @@ |
|||||
|
<!-- Generated by documentation.js. Update this documentation by updating the source code. --> |
||||
|
|
||||
|
## DataSources |
||||
|
|
||||
|
This module manages data sources within the editor. |
||||
|
You can initialize the module with the editor by passing an instance of `EditorModel`. |
||||
|
|
||||
|
```js |
||||
|
const editor = new EditorModel(); |
||||
|
const dsm = new DataSourceManager(editor); |
||||
|
``` |
||||
|
|
||||
|
Once the editor is instantiated, you can use the following API to manage data sources: |
||||
|
|
||||
|
```js |
||||
|
const dsm = editor.DataSources; |
||||
|
``` |
||||
|
|
||||
|
* [add][1] - Add a new data source. |
||||
|
* [get][2] - Retrieve a data source by its ID. |
||||
|
* [getAll][3] - Retrieve all data sources. |
||||
|
* [remove][4] - Remove a data source by its ID. |
||||
|
* [clear][5] - Remove all data sources. |
||||
|
|
||||
|
Example of adding a data source: |
||||
|
|
||||
|
```js |
||||
|
const ds = dsm.add({ |
||||
|
id: 'my_data_source_id', |
||||
|
records: [ |
||||
|
{ id: 'id1', name: 'value1' }, |
||||
|
{ id: 'id2', name: 'value2' } |
||||
|
] |
||||
|
}); |
||||
|
``` |
||||
|
|
||||
|
### Parameters |
||||
|
|
||||
|
* `em` **EditorModel** Editor model. |
||||
|
|
||||
|
## add |
||||
|
|
||||
|
Add new data source. |
||||
|
|
||||
|
### Parameters |
||||
|
|
||||
|
* `props` **[Object][6]** Data source properties. |
||||
|
* `opts` **AddOptions** (optional, default `{}`) |
||||
|
|
||||
|
### Examples |
||||
|
|
||||
|
```javascript |
||||
|
const ds = dsm.add({ |
||||
|
id: 'my_data_source_id', |
||||
|
records: [ |
||||
|
{ id: 'id1', name: 'value1' }, |
||||
|
{ id: 'id2', name: 'value2' } |
||||
|
] |
||||
|
}); |
||||
|
``` |
||||
|
|
||||
|
Returns **[DataSource]** Added data source. |
||||
|
|
||||
|
## get |
||||
|
|
||||
|
Get data source. |
||||
|
|
||||
|
### Parameters |
||||
|
|
||||
|
* `id` **[String][7]** Data source id. |
||||
|
|
||||
|
### Examples |
||||
|
|
||||
|
```javascript |
||||
|
const ds = dsm.get('my_data_source_id'); |
||||
|
``` |
||||
|
|
||||
|
Returns **[DataSource]** Data source. |
||||
|
|
||||
|
## getValue |
||||
|
|
||||
|
Get value from data sources by key |
||||
|
|
||||
|
### Parameters |
||||
|
|
||||
|
* `key` **[String][7]** Path to value. |
||||
|
* `defValue` **any**  |
||||
|
|
||||
|
Returns **any** const value = dsm.getValue('ds\_id.record\_id.propName', 'defaultValue'); |
||||
|
|
||||
|
## remove |
||||
|
|
||||
|
Remove data source. |
||||
|
|
||||
|
### Parameters |
||||
|
|
||||
|
* `id` **([String][7] | [DataSource])** Id of the data source. |
||||
|
* `opts` **RemoveOptions?**  |
||||
|
|
||||
|
### Examples |
||||
|
|
||||
|
```javascript |
||||
|
const removed = dsm.remove('DS_ID'); |
||||
|
``` |
||||
|
|
||||
|
Returns **[DataSource]** Removed data source. |
||||
|
|
||||
|
## fromPath |
||||
|
|
||||
|
Retrieve a data source, data record, and optional property path based on a string path. |
||||
|
This method parses a string path to identify and retrieve the corresponding data source |
||||
|
and data record. If a property path is included in the input, it will also be returned. |
||||
|
The method is useful for accessing nested data within data sources. |
||||
|
|
||||
|
### Parameters |
||||
|
|
||||
|
* `path` **[String][7]** The string path in the format 'dataSourceId.recordId.property'. |
||||
|
|
||||
|
### Examples |
||||
|
|
||||
|
```javascript |
||||
|
const [dataSource, dataRecord, propPath] = dsm.fromPath('my_data_source_id.record_id.myProp'); |
||||
|
// e.g., [DataSource, DataRecord, 'myProp'] |
||||
|
``` |
||||
|
|
||||
|
Returns **[DataSource?, DataRecord?, [String][7]?]** An array containing the data source, |
||||
|
data record, and optional property path. |
||||
|
|
||||
|
## store |
||||
|
|
||||
|
Store data sources to a JSON object. |
||||
|
|
||||
|
Returns **[Array][8]** Stored data sources. |
||||
|
|
||||
|
## load |
||||
|
|
||||
|
Load data sources from a JSON object. |
||||
|
|
||||
|
### Parameters |
||||
|
|
||||
|
* `data` **[Object][6]** The data object containing data sources. |
||||
|
|
||||
|
Returns **[Object][6]** Loaded data sources. |
||||
|
|
||||
|
[1]: #add |
||||
|
|
||||
|
[2]: #get |
||||
|
|
||||
|
[3]: #getall |
||||
|
|
||||
|
[4]: #remove |
||||
|
|
||||
|
[5]: #clear |
||||
|
|
||||
|
[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object |
||||
|
|
||||
|
[7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String |
||||
|
|
||||
|
[8]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array |
||||
Loading…
Reference in new issue