Skip to content

Commit 30ba3ca

Browse files
committed
Document custom handlers for Feathers events
1 parent 9542445 commit 30ba3ca

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

docs/getting-started.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ The following default options are available for configuration:
271271
const defaultOptions = {
272272
autoRemove: false, // Automatically remove records missing from responses (only use with feathers-rest)
273273
addOnUpsert: false, // Add new records pushed by 'updated/patched' socketio events into store, instead of discarding them
274-
enableEvents: true, // Listens to socket.io events when available
274+
enableEvents: true, // Listens to socket.io events when available. See the `handleEvents` API for more details
275275
idField: 'id', // The field in each record that will contain the id
276276
tempIdField: '__id',
277277
debug: false, // Set to true to enable logging messages.

docs/service-plugin.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ The following options are supported on `makeServicePlugin`.
2323
setupInstance: instance => instance, // Override this method to setup data types or related data on an instance. If using Model classes, specify this as a static class property.
2424
autoRemove: true, // Automatically remove records missing from responses (only use with feathers-rest)
2525
enableEvents: false, // Turn off socket event listeners. It's true by default
26+
handleEvents: {
27+
created: (item, { model, models }) => options.enableEvents, // handle `created` events, return true to add to the store
28+
patched: (item, { model, models }) => options.enableEvents, // handle `created` events, return true to update in the store
29+
updated: (item, { model, models }) => options.enableEvents, // handle `created` events, return true to update in the store
30+
removed: (item, { model, models }) => options.enableEvents // handle `removed` events, return true to remove from the store
31+
},
2632
addOnUpsert: true, // Add new records pushed by 'updated/patched' socketio events into store, instead of discarding them. It's false by default
2733
replaceItems: true, // If true, updates & patches replace the record in the store. Default is false, which merges in changes
2834
skipRequestIfExists: true, // For get action, if the record already exists in store, skip the remote request. It's false by default
@@ -347,6 +353,63 @@ store.dispatch('todos/remove', 1)
347353

348354
Make sure your returned records have a unique field that matches the `idField` option for the service plugin.
349355

356+
## Service Events
357+
358+
By default, the service plugin listens to all of the FeathersJS events:
359+
360+
- `created` events will add new record to the store.
361+
- `patched` events will add (if new) or update (if present) the record in the store.
362+
- `updated` events will add (if new) or update (if present) the record in the store.
363+
- `removed` events will remove the record from the store, if present.
364+
365+
This behavior can be turned off completely by passing `enableEvents: false` in either the global Feathers-Vuex options or in the service plugin options. If you configure this at the global level, the service plugin level will override it. For example, if you turn off events at the global level, you can enable them for a specific service by setting `enableEvents: true` on that service's options.
366+
367+
### Custom Event Handlers <Badge text="3.1.0+" />
368+
369+
As of version 3.1, you can customize the behavior of the event handlers, or even perform side effects based on the event data. This is handled through the new `handleEvents` option on the service plugin. Here is an example of how you might use this:
370+
371+
```js
372+
handleEvents: {
373+
created: (item, { model, models }) => {
374+
// Perform a side effect to remove any record with the same `name`
375+
const existing = Model.findInStore({ query: { name: item.name }}).data[0]
376+
if (existing) {
377+
existing.remove()
378+
}
379+
380+
// Perform side effects with other models.
381+
const { SomeModel } = models.api
382+
new SomeModel({ /* some custom data */ }).save()
383+
384+
// Access the store through model.store
385+
const modelState = model.store.state[model.namespace]
386+
if (modelState.keyedById[5]) {
387+
console.log('we accessed the vuex store')
388+
}
389+
390+
// If true, the new item will be stored.
391+
return true
392+
},
393+
updated: () => false,
394+
patched: item.hasPatchedAttribute && item.isWorthKeeping,
395+
removed: item => true // The default value, will remove the record from the store
396+
}
397+
```
398+
399+
As shown above, each handler has two possible uses:
400+
401+
1. Control the default behavior of the event by returning a boolean.
402+
- For `created`, `patched`, and `updated` a truthy return will add or update the item in the store.
403+
- For `removed` a truthy return will remove the item from the store, if present.
404+
2. Perform side effects using the current service `model` or with other `models`. The `models` object is the same as the `$FeathersVuex` object in the Vue plugin.
405+
406+
Each handler receives the following arguments:
407+
408+
- `item`: the record sent from the API server
409+
- `utils`: an object containing the following properties
410+
- `model` The current service's Model class.
411+
- `models` The same as the `$FeathersVuex` object, gives you access to each api with their respective model classes.
412+
350413
## Pagination and the `find` action
351414

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

0 commit comments

Comments
 (0)