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
+107-4Lines changed: 107 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,7 @@ Before you can use the `useFind` and `useGet` composition functions, you'll need
13
13
14
14
## Detour: Reading a TypeScript interface
15
15
16
-
The next few sections show various TypeScript interfaces, which are basically shorthand descriptions of the types of data that make up a variable. If this is your first time, here's a quick primer as an alternative to reading the [TypeScript interface docs](https://www.typescriptlang.org/docs/handbook/interfaces.html):
16
+
The next few sections show various TypeScript interfaces, which are basically shorthand descriptions of the types of data that make up a variable. In this case, they're used to show the `options` object which can be passed to each of the composition api utilities. If this is your first time with interfaces, here's a quick primer as an alternative to reading the [TypeScript interface docs](https://www.typescriptlang.org/docs/handbook/interfaces.html):
17
17
18
18
- In the [first interface example](#options), below, `UseFindOptions` is the name of the interface, similar to naming any other variable. When using TypeScript, you can import and pass them around like variables.
19
19
- Each line of the interface describes a property.
@@ -190,12 +190,89 @@ Note that with the Vue Options API (aka the only way to write components in Vue
190
190
191
191
## useGet <Badgetext="3.0.0+" />
192
192
193
-
The `useGet` utility is still being built. Docs will be written when it becomes more complete.
193
+
The `useGet` Composition API utility provides the same fall-through cache functionality as `useFind`. It has a slightly simpler API, only requiring a `model` and `id` instead of the `params` object. Still, the `params` object can be used to send along additional query parameters in the request. Below is an example of how you might use the `useGet` utility.
194
+
195
+
```html
196
+
<template>
197
+
<div>
198
+
<divv-if="post">{{ post.body }}</div>
199
+
<divv-else-if="isPending">Loading</div>
200
+
<divv-else>Post not found.</div>
201
+
</div>
202
+
</template>
203
+
204
+
<script>
205
+
import { computed } from'@vue/composition-api'
206
+
import { useFind, useGet } from'feathers-vuex'
207
+
208
+
exportdefault {
209
+
name:'BlogPostView',
210
+
props: {
211
+
id: {
212
+
type:String,
213
+
required:true
214
+
}
215
+
},
216
+
setup(props, context) {
217
+
const { Post } =context.root.$FeathersVuex.api
218
+
219
+
// Get the patient record
220
+
const { item:post, isPending } =useGet({
221
+
model: Post,
222
+
id:props.id
223
+
})
224
+
225
+
return {
226
+
post,
227
+
isPending
228
+
}
229
+
}
230
+
}
231
+
```
232
+
233
+
See the [Routing with useGet](#routing-with-useget) portion of the patterns section, below, to see how to hook up the above component to vue-router.
194
234
195
235
### Options
196
236
237
+
We learned earlier [how to read a TypeScript interface](#detour-reading-a-typescript-interface), so let's look at the TypeScript interface for the `UseGetOptions`.
238
+
239
+
```ts
240
+
interface UseGetOptions {
241
+
model:Function
242
+
id:null| string | number | Ref<null>| Ref<string>| Ref<number>
243
+
params?: Params | Ref<Params>
244
+
queryWhen?: Ref<Function>
245
+
local?: boolean
246
+
lazy?: boolean
247
+
}
248
+
```
249
+
250
+
And here's a look at each individual property:
251
+
252
+
- `model` must be a Feathers-Vuex Model class. The Model's `get` and `getFromStore` methods are used to query data.
253
+
- `id` must be a record's unique identifier (`id` or `_id`, usually) or a ref or computed property which returns one.
254
+
- When the `id` changes, the API will be queried for the new record (unless `queryWhen` evaluates to `false`).
255
+
- If the `id` is `null`, no query will be made.
256
+
- `params` is a FeathersJS Params object OR a Composition API `ref` (or `computed`, since they return a `ref` instance) which returns a Params object.
257
+
- Unlike the `useFind` utility, `useGet` does not currently have built-in debouncing.
258
+
- `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`.
259
+
- `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 `id` or `params`.
260
+
197
261
### Returned Attributes
198
262
263
+
```ts
264
+
interface UseGetData {
265
+
item: Ref<any>
266
+
servicePath: Ref<string>
267
+
isPending: Ref<boolean>
268
+
hasBeenRequested: Ref<boolean>
269
+
hasLoaded: Ref<boolean>
270
+
isLocal: Ref<boolean>
271
+
error: Ref<Error>
272
+
get:Function
273
+
}
274
+
```
275
+
199
276
## Pattens: `useFind` with `useGet`
200
277
201
278
### Simultaneous Queries
@@ -434,6 +511,32 @@ export default {
434
511
435
512
In the above example, the `patientQueryWhen` computed property will return `true` if we don't already have a `Patient` record in the store with the current `props.id`. While you could also achieve similar results by performing this logic inside of a `params` computed property, the `queryWhen` option works great as a "master override" to prevent unneeded queries.
436
513
514
+
### Routing with useGet
515
+
516
+
Apps will commonly have one or more routes with an `:id` param. This might be for viewing or editing data. Vue Router has a feature that makes it easy to write reusable components without having to directly reference the `$route` object. The key is to set the `props` attribute in a route definition to `true`. Here's an example route:
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)
539
+
437
540
## Conventions for Development
438
541
439
542
### Params are Computed
@@ -446,10 +549,10 @@ In contrast, an imperatively-written query would be a reactive object that you d
446
549
447
550
### Naming Variables
448
551
449
-
Having a variable naming convention can really assist the developer onboarding process and long run ease of use. Here are some guidelines that could be useful while using the composition API utilities:
552
+
Having a variable naming convention can really assist the developer onboarding process and long run ease of use. Here are some guidelines that could prove useful while using the composition API utilities:
450
553
451
554
- Params for `useFind` result in a list of records, and should therefore indicate plurality.
452
-
- Params for `useGet` result in a single record, and should indicate singularity.
555
+
- When used, params for `useGet` result in a single record, and should indicate singularity.
0 commit comments