Skip to content

Commit 8480b26

Browse files
Merge pull request #421 from mrfrase3/patch-1
Allow simple data modification in clone
2 parents 0d128e4 + 7bda9d6 commit 8480b26

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,28 +254,28 @@ export default function makeBaseModel(options: FeathersVuexOptions) {
254254
/**
255255
* clone the current record using the `createCopy` mutation
256256
*/
257-
public clone() {
257+
public clone(data) {
258258
const { idField, tempIdField } = this.constructor as typeof BaseModel
259259
if (this.__isClone) {
260260
throw new Error('You cannot clone a copy')
261261
}
262262
const id =
263263
getId(this, idField) != null ? getId(this, idField) : this[tempIdField]
264-
return this._clone(id)
264+
return this._clone(id, data)
265265
}
266266

267-
private _clone(id) {
267+
private _clone(id, data = {}) {
268268
const { store, namespace, _commit, _getters } = this
269269
.constructor as typeof BaseModel
270270
const { keepCopiesInStore } = store.state[namespace]
271271

272272
_commit.call(this.constructor, `createCopy`, id)
273273

274274
if (keepCopiesInStore) {
275-
return _getters.call(this.constructor, 'getCopyById', id)
275+
return Object.assign(_getters.call(this.constructor, 'getCopyById', id), data)
276276
} else {
277277
// const { copiesById } = this.constructor as typeof BaseModel
278-
return (this.constructor as typeof BaseModel).copiesById[id]
278+
return Object.assign((this.constructor as typeof BaseModel).copiesById[id], data)
279279
}
280280
}
281281
/**

test/service-module/service-module.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,25 @@ describe('Service Module', function() {
387387
)
388388
})
389389

390+
it(`allows shallow assign of data when cloning`, function() {
391+
const { serviceTodo, owners } = this
392+
let serviceTodoClone = serviceTodo.clone({ isComplete: !serviceTodo.isComplete })
393+
394+
assert.equal(
395+
!serviceTodo.isComplete,
396+
serviceTodoClone.isComplete,
397+
'clone value has changed'
398+
)
399+
400+
serviceTodoClone.commit()
401+
402+
assert.equal(
403+
serviceTodo.isComplete,
404+
true,
405+
'value has changed after commit'
406+
)
407+
})
408+
390409
it('allows reseting copy changes back to match the original', function() {
391410
const { serviceTodo } = this
392411
let serviceTodoClone = serviceTodo.clone()

0 commit comments

Comments
 (0)