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
@@ -255,30 +255,33 @@ In summary, you can plan on individual records in the action response data to be
255
255
256
256
## Basic Data Modeling with `instanceDefaults`
257
257
258
-
See the [instanceDefaults API](./model-classes.html#instanceDefaults)
258
+
See the [instanceDefaults API](./model-classes.html#instancedefaults)
259
259
260
260
## Model-Specific Computed Properties
261
261
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:
263
263
264
264
```js
265
-
exportdefaultnewVuex.Store({
266
-
plugins: [
267
-
service('post', {
268
-
instanceDefaults: {
269
-
description:'',
270
-
isComplete:false,
271
-
comments: [],
272
-
getnumberOfCommenters () {
273
-
// Put your logic here.
274
-
},
275
-
setsomeOtherProp () {
276
-
// Setters also work
277
-
}
278
-
}
279
-
})
280
-
]
281
-
})
265
+
classPostextendsBaseModel {
266
+
// Required for $FeathersVuex plugin to work after production transpile.
267
+
static modelName ='Post'
268
+
// Define default properties here
269
+
staticinstanceDefaults() {
270
+
return {
271
+
description:'',
272
+
isComplete:false,
273
+
comments: [],
274
+
}
275
+
}
276
+
277
+
// Specify computed properties as regular class properties
278
+
getnumberOfCommenters () {
279
+
// Put your logic here.
280
+
},
281
+
setsomeOtherProp () {
282
+
// Setters also work
283
+
}
284
+
}
282
285
```
283
286
284
287
## Relationships for Populated Data
@@ -326,110 +329,41 @@ Suppose a requirement is put on the `/todos` service to populate the `user` in t
326
329
327
330
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!
328
331
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:
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).
// Required for $FeathersVuex plugin to work after production transpile.
339
+
static modelName ='Todo'
340
+
// Define default properties here
341
+
staticinstanceDefaults() {
342
+
return {
343
+
email:'',
344
+
password:''
345
+
}
346
+
}
347
+
// Updates `data.user` to be an instance of the `User` class.
348
+
staticsetupInstance(data, { models }) {
349
+
if (data.user) {
350
+
data.user=newmodels.api.User(data.user)
351
+
}
352
+
return data
353
+
}
354
+
}
387
355
388
-
exportdefaultnewVuex.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
+
constservicePath='todos'
357
+
constservicePlugin=makeServicePlugin({
358
+
Model: Todo,
359
+
service:feathersClient.service(servicePath),
360
+
servicePath
404
361
})
405
362
```
406
363
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:
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.
423
365
424
-
exportdefaultnewVuex.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!
433
367
434
368
## Form Binding
435
369
@@ -549,10 +483,4 @@ export default new Vuex.Store({
549
483
550
484
## Enable Debug Logging
551
485
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