diff --git a/markdown/dev/reference/api/store/en.md b/markdown/dev/reference/api/store/en.md index 23de5bef3fa..e6557b43983 100644 --- a/markdown/dev/reference/api/store/en.md +++ b/markdown/dev/reference/api/store/en.md @@ -2,26 +2,44 @@ title: Store --- -The **Store** object holds a simple key/value store with -methods for storing and retrieving information. -A single store per pattern is shared by all pattern parts. +A **Store** object holds a simple key/value store with methods for storing and +retrieving data. + +## 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: + + + +## Notes 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 length for the collar in another part. -The `Store` object exposes the following methods: - - - - - -###### 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(); -``` - - +Include link to guide diff --git a/markdown/dev/reference/api/store/extend/en.md b/markdown/dev/reference/api/store/extend/en.md new file mode 100644 index 00000000000..05dc91d6cfd --- /dev/null +++ b/markdown/dev/reference/api/store/extend/en.md @@ -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=[]) +``` + +This method is chainable as it returns the `Store` object + +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` + + diff --git a/markdown/dev/reference/api/store/get/en.md b/markdown/dev/reference/api/store/get/en.md index 2fe44f6b302..2bc6206a55e 100644 --- a/markdown/dev/reference/api/store/get/en.md +++ b/markdown/dev/reference/api/store/get/en.md @@ -1,9 +1,46 @@ --- -title: get() +title: Store.get() --- +The `Store.get()` method retrieves the data available under `key`. + +## Signature + ```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 +``` diff --git a/markdown/dev/reference/api/store/push/en.md b/markdown/dev/reference/api/store/push/en.md new file mode 100644 index 00000000000..5bae3f92f92 --- /dev/null +++ b/markdown/dev/reference/api/store/push/en.md @@ -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, ...) +``` + +This method is [variadic](https://en.wikipedia.org/wiki/Variadic_function) + +This method is chainable as it returns the `Store` object + +## Example + +```js +const store = new Store() +store.set('example', ['Hi there']) +store.push( + 'How are you doing', + 'How are YOU doing' +) +``` + diff --git a/markdown/dev/reference/api/store/set/en.md b/markdown/dev/reference/api/store/set/en.md index 5a4de058726..0b990b919b2 100644 --- a/markdown/dev/reference/api/store/set/en.md +++ b/markdown/dev/reference/api/store/set/en.md @@ -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 -void store.set(string key, mixed value) +Store store.set(mixed key, mixed value) +``` + +This method is chainable as it returns the `Store` object + +## 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`. diff --git a/markdown/dev/reference/api/store/setifunset/en.md b/markdown/dev/reference/api/store/setifunset/en.md index fc6a5062da4..d957d2d5ab6 100644 --- a/markdown/dev/reference/api/store/setifunset/en.md +++ b/markdown/dev/reference/api/store/setifunset/en.md @@ -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 -void store.setIfUnset(string key, mixed value) +Store store.set(mixed key, mixed value) +``` + +This method is chainable as it returns the `Store` object + +## 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. diff --git a/markdown/dev/reference/api/store/unset/en.md b/markdown/dev/reference/api/store/unset/en.md new file mode 100644 index 00000000000..a8ee934e62b --- /dev/null +++ b/markdown/dev/reference/api/store/unset/en.md @@ -0,0 +1,21 @@ +--- +title: Store.unset() +--- + +The `Store.unset()` value removes a `key` from the store. + +## Signature + +```js +Store store.unset(string key) +``` + +This method is chainable as it returns the `Store` object + +## Example + +```js +const store = new Store() + .set('example', 'I will be gone before you know it') + .unset('example') +```