🎁 Upgrade to Feathers-Pinia 2.0 🎁
Feathers-Pinia 2.0 is almost ready for final release. Read the new documentation.useGetWatched
The useGetWatched Composition API utility provides the same fall-through cache functionality as useFindWatched. 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.
<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 { useGetWatched } 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 } = useGetWatched({ model: postStore.Model, id: props.id })
<script>
See the Routing with useGetWatched 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.
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:
modelmust be a Feathers-Pinia Model class. The Model'sgetandgetFromStoremethods are used to query data.idmust be a record's unique identifier (idor_id, usually) or a ref or computed property which returns one.- When the
idchanges, the API will be queried for the new record (unlessqueryWhenevaluates tofalse). - If the
idisnull, no query will be made.
- When the
paramsis a FeathersJS Params object OR a Composition APIref(orcomputed, since they return arefinstance) which returns a Params object.- Unlike the
useFindutility,useGetWatcheddoes not currently have built-in debouncing.
- Unlike the
queryWhenmust be acomputedproperty which returns aboolean. It provides a logical separation for preventing API requests apart fromnullin theid.immediate, which istrueby default, determines if the internalwatchshould fire immediately. Setimmediate: falseand the query will not fire immediately. It will only fire on subsequent changes to theidorparams.
Returned Attributes
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
}