v2.0.0-pre.18
The useGetWatched
Utility ⚠️
⚠️ warning ⚠️
This API has been replaced by the new useGet
API. To make the migration process easier, the old useGet
is now provided as useGetWatched
. This API will likely be removed in the future.
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.
The goal with the examples is to focus as much as possible on functionality and not boilerplate. As such, all examples use auto-import for Vue APIs like computed
and ref
. They also use Vue's script setup
feature. Both features come preinstalled with the Vitesse Template for Vue and the Vitesse-Feathers-Pinia Demo.
html
<template>
<div>
<div v-if="post">{{ post.body }}</div>
<div v-else-if="isPending">Loading</div>
<div v-else>Post not found.</div>
</div>
</template>
<script setup>
import { useGet } from 'feathers-pinia'
import { usePosts } from '../store/posts'
const postStore = usePosts()
const props = defineProps({
id: { type: String, required: true },
})
// Get the post record
const { item: post, isPending } = useGet({ model: postStore.Model, id: props.id })
<script>
See the Routing with useGet portion of the patterns section, below, to see how to hook up the above component to vue-router.
Options
Let's look at the TypeScript interface for the UseGetOptions
.
ts
interface UseGetOptions {
model: Function
id: null | string | number | Ref<null> | Ref<string> | Ref<number>
params?: Params | Ref<Params>
queryWhen?: Ref<Function>
local?: boolean
immediate?: boolean
}
And here's a look at each individual property:
model
must be a Feathers-Pinia Model class. The Model'sget
andgetFromStore
methods are used to query data.id
must be a record's unique identifier (id
or_id
, usually) or a ref or computed property which returns one.- When the
id
changes, the API will be queried for the new record (unlessqueryWhen
evaluates tofalse
). - If the
id
isnull
, no query will be made.
- When the
params
is a FeathersJS Params object OR a Composition APIref
(orcomputed
, since they return aref
instance) which returns a Params object.- Unlike the
useFind
utility,useGet
does not currently have built-in debouncing.
- Unlike the
queryWhen
must be acomputed
property which returns aboolean
. It provides a logical separation for preventing API requests apart fromnull
in theid
.immediate
, which istrue
by default, determines if the internalwatch
should fire immediately. Setimmediate: false
and the query will not fire immediately. It will only fire on subsequent changes to theid
orparams
.
Returned Attributes
ts
interface UseGetData {
item: Ref<any>
servicePath: Ref<string>
isPending: Ref<boolean>
hasBeenRequested: Ref<boolean>
hasLoaded: Ref<boolean>
isLocal: Ref<boolean>
error: Ref<Error>
get: Function
}