Skip to content

Commit

Permalink
Merge pull request #20 from richmolj/master
Browse files Browse the repository at this point in the history
Add syncRelationships()
  • Loading branch information
richmolj authored Mar 23, 2019
2 parents e5c96c3 + c264a53 commit de34db3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
25 changes: 25 additions & 0 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,15 @@ export class SpraypaintBase {
if (val) this.reset()
}

_onSyncRelationships?: (event: any, relationships: any) => void
private onSyncRelationships() {
if (this._onSyncRelationships) return this._onSyncRelationships
this._onSyncRelationships = (event, relationships) => {
this.relationships = relationships
}
return this._onSyncRelationships
}

_onStoreChange?: (event: any, attrs: any) => void
private onStoreChange() {
if (this._onStoreChange) return this._onStoreChange
Expand Down Expand Up @@ -514,6 +523,10 @@ export class SpraypaintBase {
if (!this.klass.sync) return
if (this.storeKey) {
EventBus.removeEventListener(this.storeKey, this.onStoreChange())
EventBus.removeEventListener(
`${this.storeKey}-sync-relationships`,
this.onSyncRelationships()
)
}

Object.keys(this.relationships).forEach(k => {
Expand All @@ -534,9 +547,21 @@ export class SpraypaintBase {
if (!this._onStoreChange) {
// not already registered
EventBus.addEventListener(this.storeKey, this.onStoreChange())
EventBus.addEventListener(
`${this.storeKey}-sync-relationships`,
this.onSyncRelationships()
)
}
}

syncRelationships(): void {
EventBus.dispatch(
`${this.storeKey}-sync-relationships`,
{},
this.relationships
)
}

reset(): void {
if (this.klass.sync) {
this.klass.store.updateOrCreate(this)
Expand Down
26 changes: 26 additions & 0 deletions test/integration/id-map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@ describe("ID Map", () => {
let author2 = (await Author.find(1)).data
expect(author1.firstName).to.eq("updated")
})

it("no longer sync on manual relationship sync", async () => {
let author1 = (await Author.find(1)).data
let book1 = new Book()
author1.books = [book1]
author1.unlisten()
let author2 = (await Author.find(1)).data
let book2 = new Book()
author2.books = [book2]
author2.syncRelationships()
expect(author1.books).to.eql([book1])
})
})

// A good way to think about this test:
Expand Down Expand Up @@ -186,6 +198,20 @@ describe("ID Map", () => {
})
})

describe("when manually syncing relationships", () => {
it("works", async () => {
let book1 = new Book()
let book2 = new Book()
let author1 = new Author({ id: 1, books: [book1] })
author1.isPersisted = true
let author2 = new Author({ id: 2, books: [book2] })
author2.isPersisted = true

author2.syncRelationships()
expect(author1.books).to.eql([book2])
})
})

describe("when updated", () => {
beforeEach(() => {
fetchMock.get("http://example.com/api/books/1", {
Expand Down
4 changes: 2 additions & 2 deletions test/unit/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1286,7 +1286,7 @@ describe("Model", () => {

it("removes event listener from self + relationships", () => {
author.unlisten()
sinon.assert.callCount(removeListenerSpy, 2)
sinon.assert.callCount(removeListenerSpy, 4)
})

describe("when sync option is false", () => {
Expand Down Expand Up @@ -1318,7 +1318,7 @@ describe("Model", () => {

it("adds event listener", () => {
author.listen()
sinon.assert.callCount(addListenerSpy, 1)
sinon.assert.callCount(addListenerSpy, 2)
})

describe("when sync option is false", () => {
Expand Down

0 comments on commit de34db3

Please sign in to comment.