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/composition-api.md
+20-5Lines changed: 20 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -105,7 +105,7 @@ interface UseFindOptions {
105
105
fetchParams?:Params|Ref<Params>
106
106
queryWhen?:Ref<Function>
107
107
qid?:string
108
-
lazy?:boolean
108
+
immediate?:boolean
109
109
}
110
110
```
111
111
@@ -123,7 +123,7 @@ And here's a look at each individual property:
123
123
- Explicitly returning `null` will prevent an API request from being made.
124
124
-`queryWhen` must be a `computed` property which returns a `boolean`. It provides a logical separation for preventing API requests *outside* of the `params`.
125
125
-`qid` allows you to specify a query identifier (used in the pagination data in the store). This can also be set dynamically by returning a `qid` in the params.
126
-
-`lazy`, which is `false` by default, determines if the internal `watch` should fire immediately. Set `lazy: true` and the query will not fire immediately. It will only fire on subsequent changes to the params.
126
+
-`immediate`, which is `true` by default, determines if the internal `watch` should fire immediately. Set `immediate: false` and the query will not fire immediately. It will only fire on subsequent changes to the params.
127
127
128
128
### Returned Attributes
129
129
@@ -208,7 +208,7 @@ If you have already used the `makeFindMixin`, the `useFind` composition function
208
208
209
209
1.`useFind` is more TypeScript friendly. Since the mixins depended on adding dynamic attribute names that wouldn't overlap, TypeScript tooling and autocomplete weren't very effective. The attributes returned from `useFind` are always consistent.
210
210
1. Instead of providing a service name, you provide a service Model from the `$FeathersVuex` Vue plugin.
211
-
1. The default behavior of `useFind` is to immediately query the API server. The `makeFindMixin`, by default, would wait until the watcher noticed the change. This is to match the default behavior of `watch` in the Vue Composition API. You can pass `{ lazy: true }` in the `useFind` options, which will be passed directly to the internal `watch` on the params.
211
+
1. The default behavior of `useFind` is to immediately query the API server. The `makeFindMixin`, by default, would wait until the watcher noticed the change. This is to match the default behavior of `watch` in the Vue Composition API. You can pass `{ immediate: false }` in the `useFind` options, which will be passed directly to the internal `watch` on the params.
212
212
213
213
Note that with the Vue Options API (aka the only way to write components in Vue 2.0) the models are found in `this.$FeathersVuex`. With the Vue Composition API, this object is now at `context.root.$FeathersVuex`.
214
214
@@ -268,7 +268,7 @@ interface UseGetOptions {
268
268
params?: Params | Ref<Params>
269
269
queryWhen?: Ref<Function>
270
270
local?: boolean
271
-
lazy?: boolean
271
+
immediate?: boolean
272
272
}
273
273
```
274
274
@@ -281,7 +281,7 @@ And here's a look at each individual property:
281
281
- `params` is a FeathersJS Params object OR a Composition API `ref` (or `computed`, since they return a `ref` instance) which returns a Params object.
282
282
- Unlike the `useFind` utility, `useGet` does not currently have built-in debouncing.
283
283
- `queryWhen` must be a `computed` property which returns a `boolean`. It provides a logical separation for preventing API requests apart from `null` in the `id`.
284
-
- `lazy`, which is `true` by default, determines if the internal `watch` should fire immediately. By default a single query will be performed, independent of the watchers. Set `lazy:false` and the watchers on the `id` and `params`will fire immediately (currently this will cause duplicate queries to be performed, so it's not recommended).
284
+
- `immediate`, which is `true` by default, determines if the internal `watch` should fire immediately. Set `immediate:false` and the query will not fire immediately. It will only fire on subsequent changes to the `id` or `params`.
285
285
286
286
### Returned Attributes
287
287
@@ -779,6 +779,21 @@ export default new Router({
779
779
780
780
Now, the `Post.vue` file only requires to have a `prop` named `id`. Vue Router will pass the params from the route as props to the component. See the [first useGet example](#useget) for a component that would work with the above route. The vue-router documentation has more information about [Passing Props to Route Components](https://router.vuejs.org/guide/essentials/passing-props.html#passing-props-to-route-components)
781
781
782
+
### Composing with Model types
783
+
784
+
Both `useGet` and `useFind` have an optional type parameter for the Model type which is used as the type for the returned item(s).
785
+
786
+
```ts
787
+
// Destructure Model class from global models object
BaseModel typing gives helpful IDE autocomplete and errors when using Model classes.
57
+
58
+
Since Feathers-Vuex doesn't know what your data looks like, you will need to help define your underlying model data's interface.
59
+
60
+
By default, Model classes are `string` indexable with value of type `any`. This isn't super helpful...
61
+
62
+
```ts
63
+
// Just like before, we define our class as an extension of BaseModel
64
+
classUserextendsBaseModel { /* ... */ }
65
+
66
+
// Augment the User Model interface
67
+
interfaceUser {
68
+
email:string
69
+
password:string
70
+
}
71
+
```
72
+
73
+
Now, whenever we access a `User` model, all fields defined in the interface will be available in IDE auto-complete/intellisense along with the model methods/props.
74
+
75
+
If you already have a User interface defined under a different name, just define a new interface with the same name as your Model class like so
76
+
77
+
```ts
78
+
// if our User interface already exists as UserRecord
79
+
interfaceUserextendsUserRecord {}
80
+
```
81
+
82
+
To further enhance typing, you can augment FeathersVuex types to aid development in other parts of your app. It's important to note the differences in the following example if you do or do not setup a `serverAlias`.
83
+
84
+
```ts
85
+
// src/store/user.store.ts
86
+
import { ServiceState } from'feathers-vuex'
87
+
88
+
classUserextendsBaseModel { /* ... */ }
89
+
interfaceUser { /* ... */ }
90
+
const servicePath ='users'
91
+
92
+
declaremodule"feathers-vuex" {
93
+
interfaceFeathersVuexStoreState {
94
+
[servicePath]:ServiceState<User>
95
+
}
96
+
97
+
// Only if you setup FeathersVuex without a serverAlias!!
98
+
interfaceFeathersVuexGlobalModels {
99
+
User:typeofUser
100
+
}
101
+
}
102
+
103
+
// Only if you setup FeathersVuex with a serverAlias!!
104
+
declaremodule"src/store" {
105
+
interfaceMyApiModels {
106
+
User:typeofUser
107
+
}
108
+
}
109
+
```
110
+
111
+
If you have setup a `serverAlias`, you need to add the following to `src/store/index.ts`.
112
+
113
+
```ts
114
+
// src/store/index.ts
115
+
exportinterfaceMyApiModels { /* Let each service augment this interface */ }
116
+
declaremodule"feathers-vuex" {
117
+
interfaceFeathersVuexGlobalModels {
118
+
'my-api-name':MyApiModels
119
+
}
120
+
}
121
+
```
122
+
123
+
Replace `my-api-name` with the `serverAlias` you used when setting up FeathersVuex.
124
+
54
125
## Model attributes
55
126
56
127
The following attributes are available on each model:
0 commit comments