Skip to content

Commit 62699e9

Browse files
committed
Allow passing lazy:true to useFind
The default behavior of `useFind` is to immediately query the API server. The `makeFindMixin`, by default, would wait until the watcher noticed the change. This is to match the default behavior of `watch` in the Vue Composition API. You can pass `{ lazy: true }` in the `useFind` options, which will be passed directly to the internal `watch` on the params.
1 parent 67045ec commit 62699e9

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

docs/composition-api.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ Before you can use the `useFind` and `useGet` composition functions, you'll need
1212

1313
## useFind <Badge text="3.0.0+" />
1414

15-
If you have already used the `makeFindMixin`, the `useFind` composition function will be very familiar, since it offers the same functionality in a more powerful way. The main difference is that instead of providing a service name, you provide a service Model from the `$FeathersVuex` Vue plugin. Note that when using the component object syntax (aka the way components are made with Vue 2.0) the models are found in `this.$FeathersVuex`. When using the Vue composition API, this object is now found in `context.root.$FeathersVuex`, as shown in the following example:
15+
If you have already used the `makeFindMixin`, the `useFind` composition function will be very familiar, since it offers the same functionality in a more powerful way. There are a few differences, though.
16+
17+
1. Instead of providing a service name, you provide a service Model from the `$FeathersVuex` Vue plugin.
18+
1. The default behavior of `useFind` is to immediately query the API server. The `makeFindMixin`, by default, would wait until the watcher noticed the change. This is to match the default behavior of `watch` in the Vue Composition API. You can pass `{ lazy: true }` in the `useFind` options, which will be passed directly to the internal `watch` on the params.
19+
20+
Note that with the Vue Options API (aka the only way to write components in Vue 2.0) the models are found in `this.$FeathersVuex`. With the Vue Composition API, this object is now at `context.root.$FeathersVuex`, as shown in the following example:
1621

1722
```html
1823
<template>

src/useFind.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ const defaults = {
1818
params: null,
1919
queryWhen: (): boolean => true,
2020
qid: 'default',
21-
local: false
21+
local: false,
22+
lazy: false
2223
}
2324

2425
interface UseFindOptions {
@@ -28,6 +29,7 @@ interface UseFindOptions {
2829
name?: string
2930
queryWhen?: Function
3031
qid?: string
32+
lazy?: boolean
3133
}
3234
interface UseFindState {
3335
debounceTime: null | number
@@ -57,7 +59,7 @@ interface UseFindData {
5759
}
5860

5961
export default function find(options: UseFindOptions): UseFindData {
60-
const { model, params, queryWhen, qid, local } = Object.assign(
62+
const { model, params, queryWhen, qid, local, lazy } = Object.assign(
6163
{},
6264
defaults,
6365
options
@@ -173,7 +175,8 @@ export default function find(options: UseFindOptions): UseFindData {
173175
() => getFetchParams(),
174176
() => {
175177
findProxy()
176-
}
178+
},
179+
{ lazy }
177180
)
178181

179182
return {

test/use/find.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,23 @@ describe('use/find', function() {
114114
assert(queryWhen.value === true)
115115
})
116116

117-
it.skip('allows passing {lazy:true} to not query immediately', function() {})
117+
it('allows passing {lazy:true} to not query immediately', function() {
118+
const { Instrument } = makeContext()
119+
120+
const instrumentParams = computed(() => {
121+
return {
122+
query: {},
123+
paginate: false
124+
}
125+
})
126+
const instrumentsData = useFind({
127+
model: Instrument,
128+
params: instrumentParams,
129+
lazy: true
130+
})
131+
const { haveBeenRequestedOnce } = instrumentsData
132+
133+
assert(isRef(haveBeenRequestedOnce))
134+
assert(haveBeenRequestedOnce.value === false)
135+
})
118136
})

0 commit comments

Comments
 (0)