Skip to content

Commit 0ca8dbf

Browse files
Merge pull request #477 from hamiltoes/docs/handle-complex-events
docs: handle complex events
2 parents 7811e65 + ab49f32 commit 0ca8dbf

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

docs/service-plugin.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,50 @@ Each handler receives the following arguments:
449449
- `model` The current service's Model class.
450450
- `models` The same as the `$FeathersVuex` object, gives you access to each api with their respective model classes.
451451

452+
You do not have to specify a handler for every event. Any that are not specified in your service-specific `handleEvents`, will fall back to using the `handleEvents` handler in your global options. If none are defined for the service or globally, the default behavior is controlled by the `enableEvents` option.
453+
454+
#### Handling complex events <Badge text="3.10.0+" />
455+
456+
If your application emits the standard Feathers service events inside a nested object with additional data, you can use `handleEvents` to tell FeathersVuex what part of that data is actually the model data that should be used to update the store.
457+
458+
To do this, use `handleEvents` as described before, but return a tuple `[affectsStore, modelData]` from your handler.
459+
460+
- `affectsStore` a truthy value indicates the event should update the store
461+
- `modelData` is the model data used to update the store
462+
463+
For example, you've configured your Feathers API to emit `patched` events for your `Todos` service that include context about the event which look like
464+
465+
```json
466+
{
467+
"$context": {
468+
"time": 1445411009000,
469+
"userId": 121,
470+
"deviceId": "Marty's iPhone"
471+
},
472+
"event": {
473+
"id": 88,
474+
"text": "Get back to the past",
475+
"done": true
476+
}
477+
}
478+
```
479+
480+
For this service to play nicely with FeathersVuex, you'll need to use `handleEvents`
481+
482+
```js
483+
handleEvents: {
484+
patched: (item, { model, models }) => {
485+
// Perform any side effects...
486+
487+
// If the first element is truthy, the item will update the store
488+
// The second element is the actual model data to add to the store
489+
return [true, item.event]
490+
}
491+
}
492+
```
493+
494+
The original event data is bubbled to [Model events](./model-classes.md#model-events) so listeners receive the full event context.
495+
452496
## Pagination and the `find` action
453497

454498
Both the `find` action and the `find` getter support pagination. There are differences in how they work.

0 commit comments

Comments
 (0)