You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/service-plugin.md
+63Lines changed: 63 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,6 +23,12 @@ The following options are supported on `makeServicePlugin`.
23
23
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.
24
24
autoRemove:true, // Automatically remove records missing from responses (only use with feathers-rest)
25
25
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
+
},
26
32
addOnUpsert:true, // Add new records pushed by 'updated/patched' socketio events into store, instead of discarding them. It's false by default
27
33
replaceItems:true, // If true, updates & patches replace the record in the store. Default is false, which merges in changes
28
34
skipRequestIfExists:true, // For get action, if the record already exists in store, skip the remote request. It's false by default
Make sure your returned records have a unique field that matches the `idField` option for the service plugin.
349
355
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 <Badgetext="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`
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
+
350
413
## Pagination and the `find` action
351
414
352
415
Both the `find` action and the `find` getter support pagination. There are differences in how they work.
0 commit comments