Working with lists
To get a list of models from your api, use the useList
function.
vue
<script setup lang="ts">
import vroom from '@/vroom';
const { items } = vroom.useList('book');
</script>
model
: The name of the model to fetchoptions
(optional): An object of options (each of these are described in further detail in later sections)sort
: An array of sort fieldsfilter
: An object of filtersinclude
: An array of relations to includepagination
(object
): Pagination optionslazy
(boolean
): If true useList will only fetch when refresh is called (default:false
)- From >=0.12.0 this can be a reactive prop
loadOnUpdate
(boolean
): If trueisLoading
will be set to true on every fetch (including refreshes) (default:false
)mergePages
(boolean
): If true any change of page/cursor in pagination will append the next page instead of swapping them (useful for infinite scrolling) (defaultfalse
)path
(string
): If set will overwrite the path being called (instead of vrooms autogenerated one)throttle
(number
ms
) (>=0.12.0): Only call the api every N ms (will debounce the last trigger so it always runs)
Return values
useList
will return the following
items
: A computed array of items returned from the api (will be an empty array while loading)isLoading
(boolean
): True while the fetching from api the first time (ifloadOnUpdate
is set totrue
it will also be true on refreshes)isFailed
(boolean
): True if the latest fetch failedrefresh
(function
): Calling this function will refetch from the apipushId
((id) => void
): If you have an item loading (e.g. something you just created) that you want to show in the list, you can use this function to add the idcreate
((data) => void
): Shorthand for creating a new item and pushing the id to the list
Autorefreshing with computed props
useList
will automatically refresh your list if it sees your filter
s, sort
og pagination
change. To do that you can pass it reactive props like ref
or computed
like so:
vue
<script setup lang="ts">
import vroom from '@/vroom';
const page = ref(1);
// If page is changed, a refetch will trigger
const { items } = vroom.useList('book', { pagination: { page }});
</script>