Skip to content

Commit de5c685

Browse files
committed
fix: copiesById should not be on the BaseModel
1 parent 4fa9006 commit de5c685

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import makeFindMixin from './make-find-mixin'
1010
import makeGetMixin from './make-get-mixin'
1111
import { globalModels as models } from './service-module/global-models'
1212
import { clients, addClient } from './service-module/global-clients'
13-
import makeModel from './service-module/make-model'
13+
import makeBaseModel from './service-module/make-base-model'
1414
import prepareMakeServicePlugin from './service-module/make-service-plugin'
1515
import prepareMakeAuthPlugin from './auth-module/make-auth-plugin'
1616
import useFind from './useFind'
@@ -53,7 +53,7 @@ export default function feathersVuex(feathers, options: FeathersVuexOptions) {
5353

5454
addClient({ client: feathers, serverAlias: options.serverAlias })
5555

56-
const BaseModel = makeModel(options)
56+
const BaseModel = makeBaseModel(options)
5757
const makeServicePlugin = prepareMakeServicePlugin(options)
5858
const makeAuthPlugin = prepareMakeAuthPlugin(feathers, options)
5959

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const defaultOptions = {
3434
*
3535
* @param options
3636
*/
37-
export default function makeModel(options: FeathersVuexOptions) {
37+
export default function makeBaseModel(options: FeathersVuexOptions) {
3838
const addModel = prepareAddModel(options)
3939
const { serverAlias } = options
4040

@@ -70,7 +70,7 @@ export default function makeModel(options: FeathersVuexOptions) {
7070
public static serverAlias: string = options.serverAlias
7171

7272
public static readonly models = globalModels // Can access other Models here
73-
public static readonly copiesById = {}
73+
public static copiesById = {}
7474

7575
public __id: string
7676
public __isClone: boolean
@@ -252,7 +252,7 @@ export default function makeModel(options: FeathersVuexOptions) {
252252
}
253253

254254
private _clone(id) {
255-
const { store, copiesById, namespace, _commit, _getters } = this
255+
const { store, namespace, _commit, _getters } = this
256256
.constructor as typeof BaseModel
257257
const { keepCopiesInStore } = store.state[namespace]
258258

@@ -261,7 +261,8 @@ export default function makeModel(options: FeathersVuexOptions) {
261261
if (keepCopiesInStore) {
262262
return _getters.call(this.constructor, 'getCopyById', id)
263263
} else {
264-
return copiesById[id]
264+
// const { copiesById } = this.constructor as typeof BaseModel
265+
return (this.constructor as typeof BaseModel).copiesById[id]
265266
}
266267
}
267268
/**

src/service-module/service-module.mutations.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,9 @@ export default function makeServiceMutations() {
263263
if (!item.hasOwnProperty('__ob__')) {
264264
item = Vue.observable(item)
265265
}
266+
if (!Model.hasOwnProperty('copiesById')) {
267+
Object.defineProperty(Model, 'copiesById', { value: {} })
268+
}
266269
Model.copiesById[id] = item
267270
}
268271
},

test/service-module/model-instance-defaults.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,29 @@ describe('Models - Default Values', function() {
180180
)
181181
})
182182

183+
it('each model has its own Model.copiesById', function() {
184+
const { Todo, Person } = makeContext()
185+
const todo = new Todo({ id: 1, description: 'This is the original' })
186+
const person = new Person({ id: 2, name: 'Xavier' })
187+
188+
todo.clone()
189+
assert(Todo.copiesById[1], 'should have a copy stored on Todo.copiesById')
190+
assert(
191+
!Person.copiesById[1],
192+
'should not have a copy stored on Person.copiesById'
193+
)
194+
195+
person.clone()
196+
assert(
197+
Person.copiesById[2],
198+
'should have a copy stored on Person.copiesById'
199+
)
200+
assert(
201+
!Todo.copiesById[2],
202+
'should not have a copy stored on Todo.copiesById'
203+
)
204+
})
205+
183206
it('allows instance defaults, including getters and setters', function() {
184207
const { BaseModel } = feathersVuex(feathersClient, {
185208
serverAlias: 'instance-defaults'

0 commit comments

Comments
 (0)