diff --git a/package.json b/package.json index 32eb50a..e096bda 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mobx-rest", - "version": "0.0.5", + "version": "0.0.6", "description": "REST conventions for mobx.", "repository": { "type": "git", diff --git a/src/Collection.js b/src/Collection.js index a19cfb9..f2ab9fc 100644 --- a/src/Collection.js +++ b/src/Collection.js @@ -1,6 +1,6 @@ /* globals Class */ // @flow -import { observable, computed } from 'mobx' +import { observable, computed, action } from 'mobx' import Model from './Model' import Api from './Api' import arrayDiff from 'lodash.difference' diff --git a/src/Model.js b/src/Model.js index 7aaae4e..36812c1 100644 --- a/src/Model.js +++ b/src/Model.js @@ -1,5 +1,5 @@ // @flow -import { observable, extendObservable, asMap } from 'mobx' +import { observable, asMap, action, ObservableMap } from 'mobx' import Collection from './Collection' import getUuid from 'node-uuid' import type { Uuid, Error, Request, Id, Label, DestroyOptions, SaveOptions } from './types' @@ -10,19 +10,20 @@ class Model { uuid: Uuid collection: Collection + attributes: ObservableMap constructor (collection: Collection, attributes: {}) { this.uuid = getUuid.v4() this.collection = collection - extendObservable(this, {attributes: asMap(attributes)}) + this.attributes = observable(asMap(attributes)) } get (attribute: string): ?any { - return this.attributes[attribute] + return this.attributes.get(attribute) } @action set (data: {}): void { - this.attributes = Object.assign(this.attributes, data) + this.attributes.merge(data) } @action save ( @@ -30,15 +31,15 @@ class Model { {optimistic = true, patch = true}: SaveOptions = {} ): Promise<*> { let data = Object.assign({}, attributes) - let originalAttributes = Object.assign({}, this.attributes) - if (!this.attributes.id) { + let originalAttributes = this.attributes.toJS() + if (!this.get('id')) { return this.collection.create(attributes, {optimistic}) } const label: Label = 'updating' if (patch) { - data = Object.assign({}, this.attributes, attributes) + data = Object.assign({}, this.attributes.toJS(), attributes) } // TODO: use PATCH @@ -47,7 +48,7 @@ class Model { data ) - if (optimistic) this.attributes = data + if (optimistic) this.attributes = asMap(data) this.request = {label, abort} @@ -58,7 +59,7 @@ class Model { }) .catch((body) => { this.request = null - this.attributes = originalAttributes + this.attributes = asMap(originalAttributes) this.error = {label, body} }) } @@ -66,7 +67,7 @@ class Model { @action destroy ( {optimistic = true}: DestroyOptions = {} ): Promise<*> { - if (!this.attributes.id) { + if (!this.get('id')) { this.collection.remove([this.uuid], {optimistic}) return Promise.resolve() } @@ -84,14 +85,14 @@ class Model { this.request = null }) .catch((body) => { - if (optimistic) this.collection.add([this.attributes]) + if (optimistic) this.collection.add([this.attributes.toJS()]) this.error = {label, body} this.request = null }) } get id (): Id { - return this.attributes.id || this.uuid + return this.get('id') || this.uuid } } diff --git a/test/ModelTest.js b/test/ModelTest.js index 893f1d7..8be98fc 100644 --- a/test/ModelTest.js +++ b/test/ModelTest.js @@ -31,7 +31,7 @@ describe('Model', () => { }) describe('get', () => { - it('finds a model at a given position', () => { + it('returns the attribute', () => { assert.equal(model.get('name'), item.name) }) }) @@ -50,7 +50,7 @@ describe('Model', () => { const name = 'dylan' context('if the item is not persisted', () => { - beforeEach(() => delete model.attributes.id) + beforeEach(() => model.attributes.delete('id')) it('it adds the model', () => { const create = sinon.stub(collection, 'create') @@ -162,7 +162,7 @@ describe('Model', () => { describe('destroy', () => { context('if the item is not persisted', () => { - beforeEach(() => delete model.attributes.id) + beforeEach(() => model.attributes.delete('id')) it('it removes the model', () => { const remove = sinon.stub(collection, 'remove')