Skip to content

Commit ddc6f77

Browse files
committed
docs: update example re: require.context
1 parent 115ae81 commit ddc6f77

File tree

1 file changed

+33
-80
lines changed

1 file changed

+33
-80
lines changed

docs/common-patterns.md

Lines changed: 33 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,27 @@ Depending on what you need to do, you may be able to solve this by [accessing th
8888
If you need the response data to already be in the store, you can use the [`afterFind` action](./service-plugin.html#afterFind-response). Here's what this looks like:
8989

9090
```js
91-
import feathersVuex from 'feathers-vuex'
92-
import feathersClient from '../../feathers-client'
93-
94-
const { service } = feathersVuex(feathersClient, { idField: '_id' })
91+
import { makeServicePlugin, BaseModel } from '../feathers-client'
9592

93+
class SpeedingTicket extends BaseModel {
94+
constructor(data, options) {
95+
super(data, options)
96+
}
97+
// Required for $FeathersVuex plugin to work after production transpile.
98+
static modelName = 'SpeedingTicket'
99+
// Define default properties here
100+
static instanceDefaults() {
101+
return {
102+
vin: '',
103+
plateState: ''
104+
}
105+
}
106+
}
96107
const servicePath = 'speeding-tickets'
97-
const servicePlugin = service(servicePath, {
98-
instanceDefaults: {
99-
vin: '',
100-
plateState: ''
101-
},
108+
const servicePlugin = makeServicePlugin({
109+
Model: SpeedingTicket,
110+
service: feathersClient.service(servicePath),
111+
servicePath,
102112
actions: {
103113
afterFind ({ commit, dispatch, getters, state }, response) {
104114
if (response.summary) {
@@ -112,11 +122,10 @@ const servicePlugin = service(servicePath, {
112122
}
113123
}
114124
})
115-
```
116125

117126
## Reactive Lists with Live Queries
118127

119-
Using Live Queries will greatly simplify app development. The `find` getter enables this feature. Here's how you might setup a component to take advantage of them. For the below example, let's create two live-query lists using two getters.
128+
Using Live Queries will greatly simplify app development. The `find` getter enables this feature. Here is how you might setup a component to take advantage of them. The next example shows how to setup two live-query lists using two getters.
120129

121130
```js
122131
import { mapState, mapGetters, mapActions } from 'vuex'
@@ -162,93 +171,37 @@ You can use the file system to organize each service into its own module. This i
162171
```js
163172
import Vue from 'vue'
164173
import Vuex from 'vuex'
165-
import feathersVuex from 'feathers-vuex'
166-
import feathersClient from '../feathers-client'
167-
168-
const { auth, FeathersVuex } = feathersVuex(feathersClient, { idField: '_id' })
174+
import { FeathersVuex } from '../feathers-client'
175+
import auth from './store.auth'
169176
170177
Vue.use(Vuex)
171178
Vue.use(FeathersVuex)
172179
173180
const requireModule = require.context(
174-
// The relative path holding the service modules
181+
// The path where the service modules live
175182
'./services',
176183
// Whether to look in subfolders
177184
false,
178-
// Only include .js files (prevents duplicate imports)
185+
// Only include .js files (prevents duplicate imports`)
179186
/.js$/
180187
)
181-
const servicePlugins = requireModule.keys().map(modulePath => requireModule(modulePath).default)
188+
const servicePlugins = requireModule
189+
.keys()
190+
.map(modulePath => requireModule(modulePath).default)
182191
183192
export default new Vuex.Store({
184193
state: {},
185-
getters: {},
186194
mutations: {},
187-
modules: {},
188-
plugins: [
189-
// Use the spread operator to register all of the imported plugins
190-
...servicePlugins,
191-
192-
auth({ userService: 'users' })
193-
]
195+
actions: {},
196+
plugins: [...servicePlugins, auth]
194197
})
195198
```
196199
197-
With the `store.js` file in place, we can start adding services to the `services` folder. Here's an example user service. Notice that this format is a clean way to use hooks, as well.
200+
With the `store.js` file in place, we can start adding services to the `services` folder.
198201
199-
```js
200-
import feathersVuex from 'feathers-vuex'
201-
import feathersClient from '../../feathers-client'
202-
203-
const { service } = feathersVuex(feathersClient, { idField: '_id' })
204-
205-
const servicePath = 'users'
206-
const servicePlugin = service(servicePath, {
207-
instanceDefaults: {
208-
email: '',
209-
password: '',
210-
roles: [],
211-
firstName: '',
212-
lastName: '',
213-
get fullName () {
214-
return `${this.firstName} ${this.lastName}`
215-
}
216-
}
217-
})
218-
219-
feathersClient.service(servicePath)
220-
.hooks({
221-
before: {
222-
all: [],
223-
find: [],
224-
get: [],
225-
create: [],
226-
update: [],
227-
patch: [],
228-
remove: []
229-
},
230-
after: {
231-
all: [],
232-
find: [],
233-
get: [],
234-
create: [],
235-
update: [],
236-
patch: [],
237-
remove: []
238-
},
239-
error: {
240-
all: [],
241-
find: [],
242-
get: [],
243-
create: [],
244-
update: [],
245-
patch: [],
246-
remove: []
247-
}
248-
})
249-
250-
export default servicePlugin
251-
```
202+
- [Learn how to setup a Vuex plugin for a Feathers service.](/api-overview.html#service-plugins)
203+
- [Learn how to setup the feathers-client.js file](/api-overview.html)
204+
- [Learn how to setup the auth plugin](/api-overview.html#auth-plugin)
252205
253206
## Actions return reactive store records
254207

0 commit comments

Comments
 (0)