Skip to content

Commit

Permalink
Fixed new iface for maps
Browse files Browse the repository at this point in the history
  • Loading branch information
masylum committed Jul 12, 2016
1 parent 60480a5 commit 05d943b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 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.5",
"version": "0.0.6",
"description": "REST conventions for mobx.",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion src/Collection.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
25 changes: 13 additions & 12 deletions src/Model.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -10,35 +10,36 @@ 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 (
attributes: {},
{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
Expand All @@ -47,7 +48,7 @@ class Model {
data
)

if (optimistic) this.attributes = data
if (optimistic) this.attributes = asMap(data)

this.request = {label, abort}

Expand All @@ -58,15 +59,15 @@ class Model {
})
.catch((body) => {
this.request = null
this.attributes = originalAttributes
this.attributes = asMap(originalAttributes)
this.error = {label, body}
})
}

@action destroy (
{optimistic = true}: DestroyOptions = {}
): Promise<*> {
if (!this.attributes.id) {
if (!this.get('id')) {
this.collection.remove([this.uuid], {optimistic})
return Promise.resolve()
}
Expand All @@ -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
}
}

Expand Down
6 changes: 3 additions & 3 deletions test/ModelTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
Expand All @@ -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')
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 05d943b

Please sign in to comment.