Skip to content

Commit d6b179f

Browse files
committed
Finish useGet docs
1 parent f952853 commit d6b179f

File tree

2 files changed

+107
-5
lines changed

2 files changed

+107
-5
lines changed

docs/composition-api.md

Lines changed: 107 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Before you can use the `useFind` and `useGet` composition functions, you'll need
1313

1414
## Detour: Reading a TypeScript interface
1515

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):
1717

1818
- 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.
1919
- 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
190190

191191
## useGet <Badge text="3.0.0+" />
192192

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+
<div v-if="post">{{ post.body }}</div>
199+
<div v-else-if="isPending">Loading</div>
200+
<div v-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+
export default {
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.
194234
195235
### Options
196236
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+
197261
### Returned Attributes
198262
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+
199276
## Pattens: `useFind` with `useGet`
200277
201278
### Simultaneous Queries
@@ -434,6 +511,32 @@ export default {
434511
435512
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.
436513
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:
517+
518+
```js
519+
// router.js
520+
import Vue from 'vue'
521+
import Router from 'vue-router'
522+
523+
Vue.use(Router)
524+
525+
export default new Router({
526+
routes: [
527+
{
528+
name: 'Post View',
529+
path: '/posts/:id',
530+
component: () =>
531+
import(/* webpackChunkName: "posts" */ './views/Post.vue'),
532+
props: true
533+
}
534+
]
535+
})
536+
```
537+
538+
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+
437540
## Conventions for Development
438541
439542
### Params are Computed
@@ -446,10 +549,10 @@ In contrast, an imperatively-written query would be a reactive object that you d
446549
447550
### Naming Variables
448551
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:
450553
451554
- 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.
453556
454557
```js
455558
import { computed } from '@vue/composition-api'

src/useGet.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ interface UseGetOptions {
1616
model: Function
1717
id: null | string | number | Ref<null> | Ref<string> | Ref<number>
1818
params?: Params | Ref<Params>
19-
fetchParams?: Params | Ref<Params>
2019
queryWhen?: Ref<Function>
2120
local?: boolean
2221
lazy?: boolean

0 commit comments

Comments
 (0)