1
+ /* eslint-disable @typescript-eslint/explicit-function-return-type */
1
2
export default {
2
3
props : {
3
4
/**
@@ -22,6 +23,29 @@ export default {
22
23
type : Object ,
23
24
default : null
24
25
} ,
26
+ /**
27
+ * If a separate query is desired to fetch data, use fetchQuery
28
+ * The watchers are automatically updated, so you don't have to write 'fetchQuery.propName'
29
+ */
30
+ fetchQuery : {
31
+ type : Object
32
+ } ,
33
+ /**
34
+ * Can be used in place of the `query` prop to provide more params. Only params.query is
35
+ * passed to the getter.
36
+ */
37
+ params : {
38
+ type : Object ,
39
+ default : null
40
+ } ,
41
+ /**
42
+ * Can be used in place of the `fetchQuery` prop to provide more params. Only params.query is
43
+ * passed to the getter.
44
+ */
45
+ fetchParams : {
46
+ type : Object ,
47
+ default : null
48
+ } ,
25
49
/**
26
50
* When `queryWhen` evaluates to false, no API request will be made.
27
51
*/
@@ -34,13 +58,6 @@ export default {
34
58
type : [ Number , String ] ,
35
59
default : null
36
60
} ,
37
- /**
38
- * If a separate query is desired to fetch data, use fetchQuery
39
- * The watchers are automatically updated, so you don't have to write 'fetchQuery.propName'
40
- */
41
- fetchQuery : {
42
- type : Object
43
- } ,
44
61
/**
45
62
* Specify which properties in the query to watch and re-trigger API requests.
46
63
*/
@@ -76,13 +93,17 @@ export default {
76
93
computed : {
77
94
item ( ) {
78
95
const getArgs = this . getArgs ( this . query )
79
-
80
96
if ( this . id ) {
81
- return (
82
- this . $store . getters [ `${ this . service } /get` ] (
83
- getArgs . length === 1 ? this . id : getArgs
84
- ) || null
85
- )
97
+ if ( getArgs . length === 1 ) {
98
+ return this . $store . getters [ `${ this . service } /get` ] ( this . id ) || null
99
+ } else {
100
+ const args = [ this . id ]
101
+ const query = getArgs [ 1 ] . query
102
+ if ( query ) {
103
+ args . push ( query )
104
+ }
105
+ return this . $store . getters [ `${ this . service } /get` ] ( args ) || null
106
+ }
86
107
} else {
87
108
return null
88
109
}
@@ -97,12 +118,14 @@ export default {
97
118
methods : {
98
119
getArgs ( queryToUse ) {
99
120
const query = queryToUse || this . fetchQuery || this . query
100
- const getArgs = [ this . id ]
121
+ const params = this . fetchParams || this . params
101
122
102
- if ( query && Object . keys ( query ) . length > 0 ) {
123
+ const getArgs = [ this . id ]
124
+ if ( params ) {
125
+ getArgs . push ( params )
126
+ } else if ( query && Object . keys ( query ) . length > 0 ) {
103
127
getArgs . push ( { query } )
104
128
}
105
-
106
129
return getArgs
107
130
} ,
108
131
getData ( ) {
@@ -121,8 +144,9 @@ export default {
121
144
`${ this . service } /get` ,
122
145
getArgs . length === 1 ? this . id : getArgs
123
146
)
124
- . then ( ( ) => {
147
+ . then ( response => {
125
148
this . isGetPending = false
149
+ return response
126
150
} )
127
151
}
128
152
}
@@ -131,17 +155,15 @@ export default {
131
155
if ( this . local || this . id === 'new' ) {
132
156
return
133
157
} else if (
134
- this . id !== null &&
135
- this . id !== undefined &&
136
- ! this . query &&
137
- ! this . fetchQuery
158
+ this . fetchQuery ||
159
+ this . query ||
160
+ this . params ||
161
+ ( this . id !== null && this . id !== undefined )
138
162
) {
139
163
return this . getData ( )
140
164
} else {
141
165
// eslint-disable-next-line no-console
142
- console . log (
143
- `No query and no id provided, so no data will be fetched.`
144
- )
166
+ console . log ( `No query and no id provided, so no data will be fetched.` )
145
167
}
146
168
}
147
169
} ,
@@ -153,7 +175,7 @@ export default {
153
175
}
154
176
if ( ! this . $store . state [ this . service ] ) {
155
177
throw new Error (
156
- `The '${ this . service } ' plugin is not registered with feathers-vuex`
178
+ `The '${ this . service } ' plugin is not registered with feathers-vuex`
157
179
)
158
180
}
159
181
@@ -162,6 +184,7 @@ export default {
162
184
if (
163
185
this . fetchQuery ||
164
186
this . query ||
187
+ this . params ||
165
188
( this . id !== null && this . id !== undefined )
166
189
) {
167
190
watch . forEach ( prop => {
0 commit comments