Skip to content

Commit 71cdd57

Browse files
committed
add geters to BaseModel
1 parent 9595496 commit 71cdd57

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

src/service-module/make-base-model.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import _get from 'lodash/get'
2121
import { EventEmitter } from 'events'
2222
import { ModelSetupContext } from './types'
2323
import { Store } from 'vuex'
24+
import { GetterName } from './service-module.getters'
2425

2526
const defaultOptions = {
2627
clone: false,
@@ -175,6 +176,34 @@ export default function makeBaseModel(options: FeathersVuexOptions) {
175176
return this
176177
}
177178

179+
/**
180+
* Calls `getter`, passing this model's ID as the parameter
181+
* @param getter name of getter to call
182+
*/
183+
private getGetterWithId(getter: GetterName): unknown {
184+
const { _getters, idField, tempIdField } = this
185+
.constructor as typeof BaseModel
186+
const id =
187+
getId(this, idField) != null ? getId(this, idField) : this[tempIdField]
188+
return _getters.call(this.constructor, getter, id)
189+
}
190+
191+
get isCreatePending(): boolean {
192+
return this.getGetterWithId('isCreatePendingById') as boolean
193+
}
194+
get isUpdatePending(): boolean {
195+
return this.getGetterWithId('isUpdatePendingById') as boolean
196+
}
197+
get isPatchPending(): boolean {
198+
return this.getGetterWithId('isPatchPendingById') as boolean
199+
}
200+
get isRemovePending(): boolean {
201+
return this.getGetterWithId('isRemovePendingById') as boolean
202+
}
203+
get isPending(): boolean {
204+
return this.getGetterWithId('isPendingById') as boolean
205+
}
206+
178207
public static getId(record: Record<string, any>): string {
179208
const { idField } = this.constructor as typeof BaseModel
180209
return getId(record, idField)
@@ -214,7 +243,7 @@ export default function makeBaseModel(options: FeathersVuexOptions) {
214243
* @param method the vuex getter name without the namespace
215244
* @param payload if provided, the getter will be called as a function
216245
*/
217-
public static _getters(name: string, idOrParams?: any, params?: any) {
246+
public static _getters(name: GetterName, idOrParams?: any, params?: any) {
218247
const { namespace, store } = this
219248

220249
if (checkNamespace(namespace, this, options.debug)) {

src/service-module/types.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,28 @@ export interface Model {
287287
* model is a clone?
288288
*/
289289
readonly __isClone?: boolean
290+
291+
/**
292+
* `Create` is currently pending on this model
293+
*/
294+
readonly isCreatePending: boolean
295+
/**
296+
* `Update` is currently pending on this model
297+
*/
298+
readonly isUpdatePending: boolean
299+
/**
300+
* `Patch` is currently pending on this model
301+
*/
302+
readonly isPatchPending: boolean
303+
/**
304+
* `Remove` is currently pending on this model
305+
*/
306+
readonly isRemovePending: boolean
307+
/**
308+
* Any method is currently pending on this model
309+
*/
310+
readonly isPending: boolean
311+
290312
/**
291313
* Creates a deep copy of the record and stores it on
292314
* `Model.copiesById`. This allows you to make changes

0 commit comments

Comments
 (0)