chore: Updating final Store docs for v3
This commit is contained in:
parent
3391859fb7
commit
098d23c3b7
7 changed files with 230 additions and 28 deletions
|
@ -2,26 +2,44 @@
|
||||||
title: Store
|
title: Store
|
||||||
---
|
---
|
||||||
|
|
||||||
The **Store** object holds a simple key/value store with
|
A **Store** object holds a simple key/value store with methods for storing and
|
||||||
methods for storing and retrieving information.
|
retrieving data.
|
||||||
A single store per pattern is shared by all pattern parts.
|
|
||||||
|
## Signature
|
||||||
|
|
||||||
|
```js
|
||||||
|
Store Store.extend(Array methods=[])
|
||||||
|
```
|
||||||
|
|
||||||
|
The constructor takes a single argument, an Array of methods to add to the
|
||||||
|
store. Each entry in the array should be an array itself holding a path in
|
||||||
|
dot notation and a method. See below for an example.
|
||||||
|
|
||||||
|
## Example
|
||||||
|
|
||||||
|
```js
|
||||||
|
function myCustomMethod() {
|
||||||
|
// Do something clever
|
||||||
|
}
|
||||||
|
|
||||||
|
const store = new Store([
|
||||||
|
["path.to.the.method", myCustomMethod ]
|
||||||
|
])
|
||||||
|
```
|
||||||
|
|
||||||
|
With the configuration above, you can call `store.path.to.the.method()` and it
|
||||||
|
will run `myCustomMethod()`.
|
||||||
|
|
||||||
|
## Methods
|
||||||
|
|
||||||
|
A Store object exposes the following methods:
|
||||||
|
|
||||||
|
<ReadMore list />
|
||||||
|
|
||||||
|
## Notes
|
||||||
|
|
||||||
A store is typically used to share information between parts. For example
|
A store is typically used to share information between parts. For example
|
||||||
the length of the neck opening in one part can be used to calculate the
|
the length of the neck opening in one part can be used to calculate the
|
||||||
length for the collar in another part.
|
length for the collar in another part.
|
||||||
|
|
||||||
The `Store` object exposes the following methods:
|
<Fixme compact>Include link to guide</Fixme>
|
||||||
|
|
||||||
<ReadMore list />
|
|
||||||
|
|
||||||
<Tip>
|
|
||||||
|
|
||||||
###### The store is available as shorthand
|
|
||||||
|
|
||||||
You can access the store instance from the [Part.shorthand](/reference/api/part/shorthand/) method;
|
|
||||||
|
|
||||||
```js
|
|
||||||
let { store } = part.shorthand();
|
|
||||||
```
|
|
||||||
|
|
||||||
</Tip>
|
|
||||||
|
|
48
markdown/dev/reference/api/store/extend/en.md
Normal file
48
markdown/dev/reference/api/store/extend/en.md
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
---
|
||||||
|
title: Store.extend()
|
||||||
|
---
|
||||||
|
|
||||||
|
The `Store.extend()` method can be used to extend the store with new
|
||||||
|
functionality by passing an array of methods and the path to the location in
|
||||||
|
the store where they should be attached. It is typically not used directly, but
|
||||||
|
rather through a plugin.
|
||||||
|
|
||||||
|
## Signature
|
||||||
|
|
||||||
|
```js
|
||||||
|
Store Store.extend(Array methods=[])
|
||||||
|
```
|
||||||
|
|
||||||
|
<Tip compact>This method is chainable as it returns the `Store` object</Tip>
|
||||||
|
|
||||||
|
The single argument should be an Array of methods to add to the
|
||||||
|
store. Each entry in the array should be an array itself holding a path in
|
||||||
|
dot notation and a method, as such:
|
||||||
|
|
||||||
|
```js
|
||||||
|
function myCustomMethod() {
|
||||||
|
// Do something clever
|
||||||
|
}
|
||||||
|
|
||||||
|
const store = new Store([
|
||||||
|
["path.to.the.method", myCustomMethod ]
|
||||||
|
])
|
||||||
|
```
|
||||||
|
|
||||||
|
With the configuration above, you can call `store.path.to.the.method()` and it
|
||||||
|
will run `myCustomMethod()`.
|
||||||
|
|
||||||
|
Stores the value of `value` in the store under key `key`.
|
||||||
|
|
||||||
|
## Notes
|
||||||
|
|
||||||
|
The Store will not allow you to extend any of the following keys:
|
||||||
|
|
||||||
|
- `set`
|
||||||
|
- `setIfUnset`
|
||||||
|
- `push`
|
||||||
|
- `unset`
|
||||||
|
- `get`
|
||||||
|
- `extend`
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,46 @@
|
||||||
---
|
---
|
||||||
title: get()
|
title: Store.get()
|
||||||
---
|
---
|
||||||
|
|
||||||
|
The `Store.get()` method retrieves the data available under `key`.
|
||||||
|
|
||||||
|
## Signature
|
||||||
|
|
||||||
```js
|
```js
|
||||||
mixed store.get(string key)
|
mixed store.get(mixed key, mixed dflt)
|
||||||
```
|
```
|
||||||
|
|
||||||
Returnes the value stored under `key`.
|
If `key` is not available, the Store will return the optional second parameter
|
||||||
|
(a default value).
|
||||||
|
|
||||||
|
## Example
|
||||||
|
|
||||||
|
```js
|
||||||
|
const store = new Store()
|
||||||
|
store.set('example', 'Hi there')
|
||||||
|
const value = store.get('example')
|
||||||
|
// value now holds 'Hi there'
|
||||||
|
```
|
||||||
|
|
||||||
|
## Notes
|
||||||
|
|
||||||
|
You can get/set nested keys either through dot-notation, or by passing an
|
||||||
|
array:
|
||||||
|
|
||||||
|
```js
|
||||||
|
const store = new Store()
|
||||||
|
|
||||||
|
store.set('my.nested.example', 'Hi there')
|
||||||
|
store.set(['my', 'other', 'nested', 'example'], 'Oh hi again')
|
||||||
|
|
||||||
|
let value
|
||||||
|
// Dot notation
|
||||||
|
value = store.get('my.nested.example') // works
|
||||||
|
value = store.get('my.other.nested.example') // works
|
||||||
|
// Using an array
|
||||||
|
value = store.get(['my', 'nested', 'example']) // works
|
||||||
|
value = store.get(['my', 'other', 'nested', 'example']) // works
|
||||||
|
// Direct access to the store object
|
||||||
|
value = store.my.nested.example // Also works
|
||||||
|
value = store.my.other.nested.example // Also works
|
||||||
|
```
|
||||||
|
|
29
markdown/dev/reference/api/store/push/en.md
Normal file
29
markdown/dev/reference/api/store/push/en.md
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
---
|
||||||
|
title: Store.push()
|
||||||
|
---
|
||||||
|
|
||||||
|
The `Store.push()` method adds the parameters you pass it to the array stored under `key`.
|
||||||
|
|
||||||
|
If `key` does not hold and Array, the Store will log a warning, but nothing will happen.
|
||||||
|
|
||||||
|
## Signature
|
||||||
|
|
||||||
|
```js
|
||||||
|
Store store.push(mixed value1, mixed value2, ...)
|
||||||
|
```
|
||||||
|
|
||||||
|
<Note compact>This method is [variadic](https://en.wikipedia.org/wiki/Variadic_function)</Note>
|
||||||
|
|
||||||
|
<Tip compact>This method is chainable as it returns the `Store` object</Tip>
|
||||||
|
|
||||||
|
## Example
|
||||||
|
|
||||||
|
```js
|
||||||
|
const store = new Store()
|
||||||
|
store.set('example', ['Hi there'])
|
||||||
|
store.push(
|
||||||
|
'How are you doing',
|
||||||
|
'How are YOU doing'
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
|
@ -1,9 +1,45 @@
|
||||||
---
|
---
|
||||||
title: set()
|
title: Store.set()
|
||||||
---
|
---
|
||||||
|
|
||||||
|
The `Store.set()` method stores the value of `value` in the store under key
|
||||||
|
`key`.
|
||||||
|
|
||||||
|
## Signature
|
||||||
|
|
||||||
```js
|
```js
|
||||||
void store.set(string key, mixed value)
|
Store store.set(mixed key, mixed value)
|
||||||
|
```
|
||||||
|
|
||||||
|
<Tip compact>This method is chainable as it returns the `Store` object</Tip>
|
||||||
|
|
||||||
|
## Example
|
||||||
|
|
||||||
|
```js
|
||||||
|
const store = new Store()
|
||||||
|
store.set('example', 'Hi there')
|
||||||
|
```
|
||||||
|
|
||||||
|
## Notes
|
||||||
|
|
||||||
|
You can get/set nested keys either through dot-notation, or by passing an
|
||||||
|
array:
|
||||||
|
|
||||||
|
```js
|
||||||
|
const store = new Store()
|
||||||
|
|
||||||
|
store.set('my.nested.example', 'Hi there')
|
||||||
|
store.set(['my', 'other', 'nested', 'example'], 'Oh hi again')
|
||||||
|
|
||||||
|
let value
|
||||||
|
// Dot notation
|
||||||
|
value = store.get('my.nested.example') // works
|
||||||
|
value = store.get('my.other.nested.example') // works
|
||||||
|
// Using an array
|
||||||
|
value = store.get(['my', 'nested', 'example']) // works
|
||||||
|
value = store.get(['my', 'other', 'nested', 'example']) // works
|
||||||
|
// Direct access to the store object
|
||||||
|
value = store.my.nested.example // Also works
|
||||||
|
value = store.my.other.nested.example // Also works
|
||||||
```
|
```
|
||||||
|
|
||||||
Stores the value of `value` in the store under key `key`.
|
|
||||||
|
|
|
@ -1,10 +1,23 @@
|
||||||
---
|
---
|
||||||
title: setIfUnset
|
title: Store.setIfUnset()
|
||||||
---
|
---
|
||||||
|
|
||||||
|
The `Store.set()` method stores the value of `value` in the store under key
|
||||||
|
`key`, but only if that key does not already hold a value.
|
||||||
|
|
||||||
|
## Signature
|
||||||
|
|
||||||
```js
|
```js
|
||||||
void store.setIfUnset(string key, mixed value)
|
Store store.set(mixed key, mixed value)
|
||||||
|
```
|
||||||
|
|
||||||
|
<Tip compact>This method is chainable as it returns the `Store` object</Tip>
|
||||||
|
|
||||||
|
## Example
|
||||||
|
|
||||||
|
```js
|
||||||
|
const store = new Store()
|
||||||
|
store.set('example', 'Hi there')
|
||||||
|
store.setIfUnset('example', 'Hi again') // This has no effect
|
||||||
```
|
```
|
||||||
|
|
||||||
Stores the value of `value` in the store under key `key`,
|
|
||||||
but only if that key does not already hold a value.
|
|
||||||
|
|
21
markdown/dev/reference/api/store/unset/en.md
Normal file
21
markdown/dev/reference/api/store/unset/en.md
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
---
|
||||||
|
title: Store.unset()
|
||||||
|
---
|
||||||
|
|
||||||
|
The `Store.unset()` value removes a `key` from the store.
|
||||||
|
|
||||||
|
## Signature
|
||||||
|
|
||||||
|
```js
|
||||||
|
Store store.unset(string key)
|
||||||
|
```
|
||||||
|
|
||||||
|
<Tip compact>This method is chainable as it returns the `Store` object</Tip>
|
||||||
|
|
||||||
|
## Example
|
||||||
|
|
||||||
|
```js
|
||||||
|
const store = new Store()
|
||||||
|
.set('example', 'I will be gone before you know it')
|
||||||
|
.unset('example')
|
||||||
|
```
|
Loading…
Add table
Add a link
Reference in a new issue