Skip to content

Commit fa38de4

Browse files
authored
Ensure additional properties get duped (#107)
1 parent 83566cf commit fa38de4

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsorm",
3-
"version": "1.3.8",
3+
"version": "1.3.9",
44
"description": "Javascript ORM",
55
"main": "lib/index.js",
66
"module": "lib-esm/index.js",

src/model.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,11 @@ export class JSORMBase {
655655
dup(): this {
656656
let attrs = Object.assign({}, this.attributes, this.relationships)
657657
let cloned = new this.klass(attrs) as any
658+
cloned.id = this.id
659+
cloned.isPersisted = this.isPersisted
660+
cloned.isMarkedForDestruction = this.isMarkedForDestruction
661+
cloned.isMarkedForDisassociation = this.isMarkedForDisassociation
662+
cloned.errors = Object.assign({}, this.errors)
658663
return cloned
659664
}
660665

test/unit/model.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1411,11 +1411,27 @@ describe("Model", () => {
14111411

14121412
describe("#dup()", () => {
14131413
it("returns a new instance of the same object", () => {
1414-
let author = new Author({ firstName: "Stephen" })
1414+
let author = new Author({ id: '1', firstName: "Stephen" })
1415+
author.isPersisted = true
1416+
author.isMarkedForDestruction = true
1417+
author.isMarkedForDisassociation = true
1418+
1419+
let errors = { firstName: { title: "asdf" } } as any
1420+
author.errors = errors
14151421
let duped = author.dup()
14161422
duped.firstName = "updated"
14171423
expect(author.firstName).to.eq("Stephen")
14181424
expect(duped.firstName).to.eq("updated")
1425+
expect(duped.id).to.eq("1")
1426+
expect(duped.isPersisted).to.eq(true)
1427+
expect(duped.isMarkedForDestruction).to.eq(true)
1428+
expect(duped.isMarkedForDisassociation).to.eq(true)
1429+
expect(duped.errors).to.deep.equal({ firstName: { title: "asdf" } })
1430+
duped.isPersisted = false
1431+
expect(author.isPersisted).to.eq(true)
1432+
let dupErrors = duped.errors as any
1433+
dupErrors.firstName = "new"
1434+
expect(author.errors.firstName).to.deep.eq({ title: "asdf" })
14191435
})
14201436

14211437
it("does not recast nonenumerables to enumerable", () => {

0 commit comments

Comments
 (0)