Skip to content

Commit a072577

Browse files
committed
docs: show how to customize the default store
Closes #408
1 parent cfca1cc commit a072577

File tree

1 file changed

+48
-33
lines changed

1 file changed

+48
-33
lines changed

docs/service-plugin.md

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -642,39 +642,54 @@ The `find` getter queries data from the local store using the same Feathers quer
642642
As shown in the example below, the service module allows you to customize its store:
643643

644644
```js
645-
const store = new Vuex.Store({
646-
plugins: [
647-
// Add custom state, getters, mutations, or actions, if needed
648-
service('things', {
649-
state: {
650-
test: true
651-
},
652-
getters: {
653-
getSomeData () {
654-
return 'some data'
655-
}
656-
},
657-
mutations: {
658-
setTest (state, val) {
659-
state.test = val;
660-
},
661-
},
662-
actions: {
663-
// Overwriting the built-in `afterFind` action.
664-
afterFind ({ commit, dispatch, getters, state }, response) {
665-
// Do something with the response.
666-
// Keep in mind that the data is already in the store.
667-
},
668-
asyncStuff ({ state, getters, commit, dispatch }, args) {
669-
commit('setTestToTrue')
670-
return new Promise.resolve("")
671-
}
672-
}
673-
})
674-
]
645+
// src/store/services/users.js
646+
import feathersClient, { makeServicePlugin, BaseModel } from '../../feathers-client'
647+
648+
class Asset extends BaseModel {
649+
constructor(data, options) {
650+
super(data, options)
651+
}
652+
// Required for $FeathersVuex plugin to work after production transpile.
653+
static modelName = 'Asset'
654+
// Define default properties here
655+
static instanceDefaults() {
656+
return {
657+
email: '',
658+
password: ''
659+
}
660+
}
661+
}
662+
663+
const servicePath = 'assets'
664+
const servicePlugin = makeServicePlugin({
665+
Model: Asset,
666+
service: feathersClient.service(servicePath),
667+
servicePath,
668+
state: {
669+
test: true
670+
},
671+
getters: {
672+
getSomeData () {
673+
return 'some data'
674+
}
675+
},
676+
mutations: {
677+
setTest (state, val) {
678+
state.test = val;
679+
},
680+
},
681+
actions: {
682+
// Overwriting the built-in `afterFind` action.
683+
afterFind ({ commit, dispatch, getters, state }, response) {
684+
// Do something with the response.
685+
// Keep in mind that the data is already in the store.
686+
},
687+
asyncStuff ({ state, getters, commit, dispatch }, args) {
688+
commit('setTestToTrue')
689+
return new Promise.resolve("")
690+
}
691+
}
675692
})
676693

677-
assert(store.getters['todos/oneTwoThree'] === 123, 'the custom getter was available')
678-
store.dispatch('todos/trigger')
679-
assert(store.state.todos.isTrue === true, 'the custom action was run')
694+
export default servicePlugin
680695
```

0 commit comments

Comments
 (0)