Skip to content

Commit f6abd3d

Browse files
committed
Update datastore docs
1 parent a681d20 commit f6abd3d

File tree

2 files changed

+46
-15
lines changed

2 files changed

+46
-15
lines changed

docs/reference/architecture.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,7 @@ For example:
130130

131131
Page Builder stores data for content types in a simple object called the DataStore: `Magento_PageBuilder/js/data-store`.
132132

133-
The `name` parameter from the [content type configuration](configurations.md) is the name of a parameter in the `DataStore`.
134-
You can use the `subscribe` method to listen for changes in the DataStore and perform custom actions on the data.
133+
The name parameter from the [content type configuration](configurations.md) (or storage_key if specified) is the name of a parameter in the DataStore. You can use the subscribe method to listen for changes in the DataStore and perform custom actions like updating the UI.
135134

136135
This is how the system binds the data from the DataStore to your elements attribute or style. When you include a new attribute or style, you must specify where it should retrieve its data from the content type. These `name` values normally bind to the field names within UI component forms. The field names are the keys we use to save the data.
137136

docs/reference/data-store.md

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,68 @@
11
# Page Builder data store
22

33
## Summary
4-
The data store's purpose is to store all configured data associated with a content type's instance. You can modify this data in several ways, including through live edit on the Admin stage or from the content type's UI component form editor.
5-
6-
Page Builder accesses the data store when generating the data sets in the preview and master templates before render.
4+
The data store is a utility class that stores the user's input data for a content type. When Page Builder creates a content type, either from the master storage format or from a user dragging and dropping a content type, Page Builder also creates a data store instance for that content type. Whenever the user modifies a content type, either by editing it on the stage or from the UI component form, Page Builder updates the data store with the new data. The master format and preview both fetch the data required for their render from the data store.
75

86
## Access
97

10-
The data store is available for access on the `ContentType` implementation via the key `dataStore`. If you're within a preview or master component, you must first access the content type through the `contentType` property.
8+
The data store is available for access on the `ContentType` implementation, using the key `dataStore`. If you are within a preview or master component, you must first access the content type through the `contentType` property, as follows:
9+
10+
```js
11+
Preview.contentType.dataStore;
12+
```
13+
14+
## Events
15+
16+
The data store maintains its own event system. It does not use the global events system used by Page Builder. As a result, you need to create an instance of `dataStore` to observe events, as follows:
17+
18+
```js
19+
var dataStore = self.contentType.dataStore;
20+
21+
dataStore.events.on("state", function (state) {
22+
console.log(state);
23+
});
24+
```
1125

1226
## API
27+
1328
#### `get(key: string, defaultValue?: any): any`
29+
1430
Retrieve a single value from the content type's data store based on its key. You can provide an optional default value if no data is present.
1531

1632
#### `getState(): object`
33+
1734
Get the entire state object for the current content type instance.
1835

1936
#### `set(key: string, value: any): void`
37+
2038
Set a single piece of data into the content type's data store.
2139

2240
#### `setState(): void`
41+
2342
Set the entire state for the current content type.
2443

25-
## Events
26-
The data store maintains its own events system and does not use the global events system that's otherwise used by Page Builder.
44+
## Usage
2745

28-
Because of this, you need to create an instance of `dataStore` to observe events.
46+
You can use the data store to modify your content type when its data changes. For example, the Page Builder native [Tabs content type](magento2-page-builder/app/code/Magento/PageBuilder/view/adminhtml/web/ts/js/content-type/tabs/preview.ts) subscribes to the data store and makes updates to it when a tab-item changes, as follows:
2947

30-
Page Builder fires a single event called `state`, which returns the entire state of the data store. Page Builder fires this event whenever the state changes.
48+
```typescript
49+
/**
50+
* Update data store with active options
51+
*/
52+
private updateTabNamesInDataStore() {
53+
const activeOptions: ActiveOptionsInterface[] = [];
54+
this.contentType.children().forEach((tab: ContentTypeInterface, index: number) => {
55+
const tabData = tab.dataStore.getState();
56+
activeOptions.push({
57+
label: tabData.tab_name.toString(),
58+
labeltitle: tabData.tab_name.toString(),
59+
value: index,
60+
});
61+
});
3162

32-
```js
33-
dataStore.events.on("state", function (state) {
34-
console.log(state);
35-
});
36-
```
63+
this.contentType.dataStore.set(
64+
"_default_active_options",
65+
activeOptions,
66+
);
67+
}
68+
```

0 commit comments

Comments
 (0)