|
1 | 1 | # Page Builder data store
|
2 | 2 |
|
3 | 3 | ## 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 | 4 |
|
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. |
7 | 6 |
|
8 | 7 | ## Access
|
9 | 8 |
|
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 | +``` |
11 | 26 |
|
12 | 27 | ## API
|
| 28 | + |
13 | 29 | #### `get(key: string, defaultValue?: any): any`
|
| 30 | + |
14 | 31 | 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.
|
15 | 32 |
|
16 | 33 | #### `getState(): object`
|
| 34 | + |
17 | 35 | Get the entire state object for the current content type instance.
|
18 | 36 |
|
19 | 37 | #### `set(key: string, value: any): void`
|
| 38 | + |
20 | 39 | Set a single piece of data into the content type's data store.
|
21 | 40 |
|
22 | 41 | #### `setState(): void`
|
| 42 | + |
23 | 43 | Set the entire state for the current content type.
|
24 | 44 |
|
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 |
27 | 46 |
|
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: |
29 | 48 |
|
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 | +... |
31 | 55 |
|
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