Skip to content

Commit 82ce214

Browse files
committed
docs: finish updating common patterns page
1 parent c9f0af1 commit 82ce214

File tree

1 file changed

+50
-122
lines changed

1 file changed

+50
-122
lines changed

docs/common-patterns.md

Lines changed: 50 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -255,30 +255,33 @@ In summary, you can plan on individual records in the action response data to be
255255

256256
## Basic Data Modeling with `instanceDefaults`
257257

258-
See the [instanceDefaults API](./model-classes.html#instanceDefaults)
258+
See the [instanceDefaults API](./model-classes.html#instancedefaults)
259259

260260
## Model-Specific Computed Properties
261261

262-
You may find yourself in a position where model-specific computed properties would be very useful. [github issue](https://github.com/feathers-plus/feathers-vuex/issues/163) This is already possible using es5 accessors. You can use both getters and setters inside `instanceDefaults`:
262+
You may find yourself in a position where model-specific computed properties would be very useful. [github issue](https://github.com/feathers-plus/feathers-vuex/issues/163). In Feathers-Vuex 1.7, these could be specified in the `instanceDefaults`. As of 2.0, they are specified directly on each Model class:
263263

264264
```js
265-
export default new Vuex.Store({
266-
plugins: [
267-
service('post', {
268-
instanceDefaults: {
269-
description: '',
270-
isComplete: false,
271-
comments: [],
272-
get numberOfCommenters () {
273-
// Put your logic here.
274-
},
275-
set someOtherProp () {
276-
// Setters also work
277-
}
278-
}
279-
})
280-
]
281-
})
265+
class Post extends BaseModel {
266+
// Required for $FeathersVuex plugin to work after production transpile.
267+
static modelName = 'Post'
268+
// Define default properties here
269+
static instanceDefaults() {
270+
return {
271+
description: '',
272+
isComplete: false,
273+
comments: [],
274+
}
275+
}
276+
277+
// Specify computed properties as regular class properties
278+
get numberOfCommenters () {
279+
// Put your logic here.
280+
},
281+
set someOtherProp () {
282+
// Setters also work
283+
}
284+
}
282285
```
283286

284287
## Relationships for Populated Data
@@ -326,110 +329,41 @@ Suppose a requirement is put on the `/todos` service to populate the `user` in t
326329

327330
Can you see the problem that will occur with this response? When this record is put into the `/todos` store, it will contain a copy of the user record. But we already have the user record in the `/users` store. And what happens when the user data changes? Now it's out of sync. To keep it in sync, you might have to manually listen for `users updated` & `users patched` events. Then you might have to write a custom mutation to update the user record attached to every applicable `todo` record. This gets messy, fast!
328331

329-
There's an easier way to solve this problem. The introduction of `instanceDefaults` allowed for another awesome feature: Model Relationships! To setup a relationship, specify a Model name, as a string, to any property, like this:
330-
331-
```js
332-
instanceDefaults: {
333-
description: '',
334-
complete: false,
335-
userId: null,
336-
user: 'User'
337-
}
338-
```
339-
340-
When this record is instantiated, the `user` attribute will first be turned into a User [model instance](./model-classes.html), stored properly in the `/users` store. The `todo.user` attribute will be a reference to that user. No more duplicate data! Here's an example of how to set this up. The following example specifies that Todo instances can have a `user` attribute that contains a `User` Model instance:
341-
342-
```js
343-
import Vue from 'vue'
344-
import Vuex from 'vuex'
345-
import feathersVuex from 'feathers-vuex'
346-
import feathersClient from './feathers-client'
347-
348-
const { service, auth, FeathersVuex } = feathersVuex(feathersClient, { idField: '_id' })
349-
350-
Vue.use(FeathersVuex)
351-
Vue.use(Vuex)
352-
353-
export default new Vuex.Store({
354-
plugins: [
355-
service('todos', {
356-
instanceDefaults: {
357-
description: '',
358-
complete: false,
359-
userId: null,
360-
user: 'User'
361-
}
362-
}),
363-
service('users', {
364-
instanceDefaults: {
365-
email: '',
366-
name: ''
367-
}
368-
})
369-
]
370-
})
371-
```
372-
373-
There's another amazing benefit from these relationships. Because `feathers-vuex` listens to real-time events and keeps data up to date, when the user record changes, the `todo.user` automatically updates!
374-
375-
It's worth noting that this feature also supports arrays. Suppose you had `/users` and `/todos` services, and your `/users` service also returned a `todos` attribute on each record. The setup would look like this:
332+
There's an easier way to solve this problem. Use the new [`setupInstance` method on Model classes](/model-classes.html#setupinstance).
376333

377334
```js
378-
import Vue from 'vue'
379-
import Vuex from 'vuex'
380-
import feathersVuex from 'feathers-vuex'
381-
import feathersClient from './feathers-client'
335+
import feathersClient, { makeServicePlugin, BaseModel } from '../feathers-client'
382336

383-
const { service, auth, FeathersVuex } = feathersVuex(feathersClient, { idField: '_id' })
384-
385-
Vue.use(FeathersVuex)
386-
Vue.use(Vuex)
337+
class Todo extends BaseModel {
338+
// Required for $FeathersVuex plugin to work after production transpile.
339+
static modelName = 'Todo'
340+
// Define default properties here
341+
static instanceDefaults() {
342+
return {
343+
email: '',
344+
password: ''
345+
}
346+
}
347+
// Updates `data.user` to be an instance of the `User` class.
348+
static setupInstance(data, { models }) {
349+
if (data.user) {
350+
data.user = new models.api.User(data.user)
351+
}
352+
return data
353+
}
354+
}
387355

388-
export default new Vuex.Store({
389-
plugins: [
390-
service('todos', {
391-
instanceDefaults: {
392-
description: '',
393-
isComplete: false
394-
}
395-
}),
396-
service('users', {
397-
instanceDefaults: {
398-
email: '',
399-
name: '',
400-
todos: 'Todo'
401-
}
402-
})
403-
]
356+
const servicePath = 'todos'
357+
const servicePlugin = makeServicePlugin({
358+
Model: Todo,
359+
service: feathersClient.service(servicePath),
360+
servicePath
404361
})
405362
```
406363

407-
With the `instanceDefaults` shown above, any `todos` returned on the `users` service would be stored in the `/todos` service store and would always be Todo instances.
408-
409-
## Reactive User Data in Auth Store
410-
411-
The `user` record in the auth store is now fully reactive and will automatically update with real-time events. In fact, the record in the auth store is the record in the users store. Please note that if you configure the `userService` option on the `auth` plugin, you must also use the `service` plugin for the `/users` service. The paths must match:
412-
413-
```js
414-
import Vue from 'vue'
415-
import Vuex from 'vuex'
416-
import feathersVuex from 'feathers-vuex'
417-
import feathersClient from './feathers-client'
418-
419-
const { service, auth, FeathersVuex } = feathersVuex(feathersClient, { idField: '_id' })
420-
421-
Vue.use(FeathersVuex)
422-
Vue.use(Vuex)
364+
When this record is instantiated, the `user` attribute will first be turned into a User [model instance](./model-classes.html), stored properly in the `/users` store. The `todo.user` attribute will be a reference to that user. No more duplicate data! Here's an example of how to set this up.
423365

424-
export default new Vuex.Store({
425-
plugins: [
426-
service('users'),
427-
auth({
428-
userService: 'users'
429-
})
430-
]
431-
})
432-
```
366+
There's another amazing benefit from these relationships. Because `feathers-vuex` listens to real-time events and keeps data up to date, when the user record changes, the `todo.user` automatically updates!
433367

434368
## Form Binding
435369

@@ -549,10 +483,4 @@ export default new Vuex.Store({
549483

550484
## Enable Debug Logging
551485

552-
If items aren't not getting added to the store properly, try setting the `debug` option on the service. It enables some additional logging that may be useful for troubleshooting:
553-
554-
```js
555-
service('todos', {
556-
debug: true
557-
})
558-
```
486+
If items aren't not getting added to the store properly, try setting the `debug` option on the `makeServicePlugin` to `true`. It enables some additional logging that may be useful for troubleshooting.

0 commit comments

Comments
 (0)