Skip to content

Commit 9e20be3

Browse files
authored
Merge pull request #289 from magento-obsessive-owls/MC-19861
MC-19861: Update datastore docs
2 parents f7930e5 + 8fb4f9b commit 9e20be3

File tree

2 files changed

+52
-14
lines changed

2 files changed

+52
-14
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: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,75 @@
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.
54

6-
Page Builder accesses the data store when generating the data sets in the preview and master templates before render.
5+
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.
76

87
## Access
98

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.
9+
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:
10+
11+
```js
12+
Preview.contentType.dataStore;
13+
```
14+
15+
## Events
16+
17+
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` and subscribe to its data changes, as follows:
18+
19+
```js
20+
var dataStore = self.contentType.dataStore;
21+
22+
dataStore.subscribe(() => {
23+
// handler to update content type
24+
});
25+
```
1126

1227
## API
28+
1329
#### `get(key: string, defaultValue?: any): any`
30+
1431
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.
1532

1633
#### `getState(): object`
34+
1735
Get the entire state object for the current content type instance.
1836

1937
#### `set(key: string, value: any): void`
38+
2039
Set a single piece of data into the content type's data store.
2140

2241
#### `setState(): void`
42+
2343
Set the entire state for the current content type.
2444

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.
45+
## Usage
2746

28-
Because of this, you need to create an instance of `dataStore` to observe events.
47+
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 show here in TypeScript:
2948

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.
49+
```typescript
50+
...
51+
args.contentType.dataStore.subscribe(() => {
52+
this.updateTabNamesInDataStore();
53+
});
54+
...
3155

32-
```js
33-
dataStore.events.on("state", function (state) {
34-
console.log(state);
35-
});
36-
```
56+
/**
57+
* Update data store with active options
58+
*/
59+
private updateTabNamesInDataStore() {
60+
const activeOptions: ActiveOptionsInterface[] = [];
61+
this.contentType.children().forEach((tab: ContentTypeInterface, index: number) => {
62+
const tabData = tab.dataStore.getState();
63+
activeOptions.push({
64+
label: tabData.tab_name.toString(),
65+
labeltitle: tabData.tab_name.toString(),
66+
value: index,
67+
});
68+
});
69+
70+
this.contentType.dataStore.set(
71+
"_default_active_options",
72+
activeOptions,
73+
);
74+
}
75+
```

0 commit comments

Comments
 (0)