Skip to content

Commit

Permalink
Fixed promises when rejecting
Browse files Browse the repository at this point in the history
  • Loading branch information
masylum committed Aug 27, 2016
1 parent 6b56132 commit 8f84871
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 65 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mobx-rest",
"version": "0.0.12",
"version": "0.0.13",
"description": "REST conventions for mobx.",
"repository": {
"type": "git",
Expand Down
60 changes: 34 additions & 26 deletions src/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,21 +125,25 @@ class Collection {
model.request = {label, abort}
}

return promise
.then((data) => {
if (model) {
model.set(data)
model.request = null
} else {
this.add([data])
}

return data
})
.catch((body) => {
if (model) this.remove([model.id])
this.error = {label, body}
})
return new Promise((resolve, reject) => {
promise
.then((data) => {
if (model) {
model.set(data)
model.request = null
} else {
this.add([data])
}

resolve(data)
})
.catch((body) => {
if (model) this.remove([model.id])
this.error = {label, body}

reject(body)
})
})
}

/**
Expand All @@ -155,17 +159,21 @@ class Collection {

this.request = {label, abort}

return promise
.then((data) => {
this.request = null
this.set(data, options)

return data
})
.catch((body) => {
this.request = null
this.error = {label, body}
})
return new Promise((resolve, reject) => {
promise
.then((data) => {
this.request = null
this.set(data, options)

resolve(data)
})
.catch((body) => {
this.request = null
this.error = {label, body}

reject(body)
})
})
}
}

Expand Down
54 changes: 32 additions & 22 deletions src/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,22 @@ class Model {

this.request = {label, abort}

return promise
.then((data) => {
this.request = null
this.set(data)

return data
})
.catch((body) => {
this.request = null
this.attributes = asMap(originalAttributes)
this.error = {label, body}
})
return new Promise((resolve, reject) => {
promise
.then((data) => {
this.request = null
this.set(data)

resolve(data)
})
.catch((body) => {
this.request = null
this.attributes = asMap(originalAttributes)
this.error = {label, body}

reject(body)
})
})
}

@action destroy (
Expand All @@ -87,16 +91,22 @@ class Model {

this.request = {label, abort}

return promise
.then(() => {
if (!optimistic) this.collection.remove([this.id])
this.request = null
})
.catch((body) => {
if (optimistic) this.collection.add([this.attributes.toJS()])
this.error = {label, body}
this.request = null
})
return new Promise((resolve, reject) => {
return promise
.then(() => {
if (!optimistic) this.collection.remove([this.id])
this.request = null

resolve()
})
.catch((body) => {
if (optimistic) this.collection.add([this.attributes.toJS()])
this.error = {label, body}
this.request = null

reject(body)
})
})
}

get id (): Id {
Expand Down
10 changes: 5 additions & 5 deletions test/CollectionTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ describe('Collection', () => {
beforeEach(reject)

it('sets the error', () => {
return collection.create(newItem).then(() => {
return collection.create(newItem).catch(() => {
assert.equal(collection.error.label, 'creating')
assert.equal(collection.error.body, error)
})
})

it('removes the model', () => {
return collection.create(newItem).then(() => {
return collection.create(newItem).catch(() => {
assert.equal(collection.models.length, 1)
})
})
Expand Down Expand Up @@ -113,7 +113,7 @@ describe('Collection', () => {
beforeEach(reject)

it('sets the error', () => {
return collection.create(newItem, {optimistic: false}).then(() => {
return collection.create(newItem, {optimistic: false}).catch(() => {
assert.equal(collection.error.label, 'creating')
assert.equal(collection.error.body, error)
})
Expand All @@ -126,7 +126,7 @@ describe('Collection', () => {
})

it('adds data from the server', () => {
return collection.create(newItem, {optimistic: false}).then(() => {
return collection.create(newItem, {optimistic: false}).catch(() => {
assert.equal(collection.models.length, 2)
assert.equal(collection.at(1).get('name'), 'dylan')
})
Expand All @@ -145,7 +145,7 @@ describe('Collection', () => {
beforeEach(reject)

it('sets the error', () => {
return collection.fetch().then(() => {
return collection.fetch().catch(() => {
assert.equal(collection.error.label, 'fetching')
assert.equal(collection.error.body, error)
})
Expand Down
22 changes: 11 additions & 11 deletions test/ModelTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,22 @@ describe('Model', () => {
beforeEach(reject)

it('sets the error', () => {
return model.save({name}).then(() => {
return model.save({name}).catch(() => {
assert.equal(model.error.label, 'updating')
assert.equal(model.error.body, error)
})
})

it('rolls back the changes', () => {
return model.save({name}).then(() => {
return model.save({name}).catch(() => {
assert.equal(model.get('name'), item.name)
assert.equal(model.get('album'), item.album)
assert.equal(model.request, null)
})
})

it('nullifies the request', () => {
return model.save({name}).then(() => {
return model.save({name}).catch(() => {
assert.equal(model.request, null)
})
})
Expand Down Expand Up @@ -127,14 +127,14 @@ describe('Model', () => {
beforeEach(reject)

it('sets the error', () => {
return model.save({name}, {optimistic: false}).then(() => {
return model.save({name}, {optimistic: false}).catch(() => {
assert.equal(model.error.label, 'updating')
assert.equal(model.error.body, error)
})
})

it('nullifies the request', () => {
return model.save({name}).then(() => {
return model.save({name}).catch(() => {
assert.equal(model.request, null)
})
})
Expand Down Expand Up @@ -181,21 +181,21 @@ describe('Model', () => {
beforeEach(reject)

it('sets the error', () => {
return model.destroy().then(() => {
return model.destroy().catch(() => {
assert.equal(model.error.label, 'destroying')
assert.equal(model.error.body, error)
})
})

it('rolls back the changes', () => {
return model.destroy().then(() => {
return model.destroy().catch(() => {
assert.equal(collection.models.length, 1)
assert.equal(collection.at(0).get('name'), item.name)
})
})

it('nullifies the request', () => {
return model.destroy().then(() => {
return model.destroy().catch(() => {
assert.equal(model.request, null)
})
})
Expand All @@ -217,20 +217,20 @@ describe('Model', () => {
beforeEach(reject)

it('sets the error', () => {
return model.destroy({optimistic: false}).then(() => {
return model.destroy({optimistic: false}).catch(() => {
assert.equal(model.error.label, 'destroying')
assert.equal(model.error.body, error)
})
})

it('rolls back the changes', () => {
return model.destroy({optimistic: false}).then(() => {
return model.destroy({optimistic: false}).catch(() => {
assert.equal(collection.models.length, 1)
})
})

it('nullifies the request', () => {
return model.destroy({optimistic: false}).then(() => {
return model.destroy({optimistic: false}).catch(() => {
assert.equal(model.request, null)
})
})
Expand Down

0 comments on commit 8f84871

Please sign in to comment.