From 8f848710842af32d3bb78edc7c97495abee26535 Mon Sep 17 00:00:00 2001 From: Pau Ramon Revilla Date: Sat, 27 Aug 2016 17:30:16 +0200 Subject: [PATCH] Fixed promises when rejecting --- package.json | 2 +- src/Collection.js | 60 ++++++++++++++++++++++++------------------ src/Model.js | 54 +++++++++++++++++++++---------------- test/CollectionTest.js | 10 +++---- test/ModelTest.js | 22 ++++++++-------- 5 files changed, 83 insertions(+), 65 deletions(-) diff --git a/package.json b/package.json index 3820d1d..4a76f27 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mobx-rest", - "version": "0.0.12", + "version": "0.0.13", "description": "REST conventions for mobx.", "repository": { "type": "git", diff --git a/src/Collection.js b/src/Collection.js index 566e1d4..7092b25 100644 --- a/src/Collection.js +++ b/src/Collection.js @@ -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) + }) + }) } /** @@ -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) + }) + }) } } diff --git a/src/Model.js b/src/Model.js index dae3572..a59e49d 100644 --- a/src/Model.js +++ b/src/Model.js @@ -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 ( @@ -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 { diff --git a/test/CollectionTest.js b/test/CollectionTest.js index 089549d..30d8ae2 100644 --- a/test/CollectionTest.js +++ b/test/CollectionTest.js @@ -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) }) }) @@ -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) }) @@ -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') }) @@ -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) }) diff --git a/test/ModelTest.js b/test/ModelTest.js index daaa5e5..b188efa 100644 --- a/test/ModelTest.js +++ b/test/ModelTest.js @@ -82,14 +82,14 @@ 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) @@ -97,7 +97,7 @@ describe('Model', () => { }) it('nullifies the request', () => { - return model.save({name}).then(() => { + return model.save({name}).catch(() => { assert.equal(model.request, null) }) }) @@ -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) }) }) @@ -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) }) }) @@ -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) }) })