Skip to content

Commit

Permalink
Added isNew and also urlRoot
Browse files Browse the repository at this point in the history
  • Loading branch information
masylum committed Feb 14, 2017
1 parent 8ced2ae commit 4deec89
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 19 deletions.
53 changes: 39 additions & 14 deletions __tests__/Model.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,8 @@ class MyCollection extends Collection {
}

class MyModel extends Model {
url () {
const id = this.has('id') && this.get('id')
if (id) {
return `/resources/${id}`
} else {
return '/resources'
}
urlRoot () {
return '/resources'
}
}

Expand All @@ -46,16 +41,46 @@ describe('Model', () => {
model = collection.at(0)
})

describe('isNew', () => {
it('returns true if it does not have an id', () => {
const newModel = new MyModel({})
expect(newModel.isNew).toBe(true)
})

it('returns false if it does not have an id', () => {
const newModel = new MyModel({ id: 4 })
expect(newModel.isNew).toBe(false)
})
})

describe('url', () => {
it('returns the collection one', () => {
expect(model.url()).toBe('/resources/1')
describe('when the model has a collection', () => {
it('returns the collection one', () => {
expect(model.url()).toBe('/resources/1')
})
})

it('throws if the model does not have a collection', () => {
expect(() => {
const newModel = new Model({ id: 1 })
newModel.url()
}).toThrowError()
describe('when the model has no collection', () => {
describe('and no urlRoot', () => {
it('throws', () => {
expect(() => {
const newModel = new Model({ id: 1 })
newModel.url()
}).toThrowError()
})
})

describe('and urlRoot is defined', () => {
it('returns different urls depending whether is new or not', () => {
let newModel

newModel = new MyModel({})
expect(newModel.url()).toBe('/resources')

newModel = new MyModel({ id: 3 })
expect(newModel.url()).toBe('/resources/3')
})
})
})
})

Expand Down
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": "1.0.2",
"version": "1.0.3",
"description": "REST conventions for mobx.",
"repository": {
"type": "git",
Expand Down
48 changes: 44 additions & 4 deletions src/Model.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
// @flow
import { observable, asMap, asFlat, action, asReference, ObservableMap, runInAction } from 'mobx'
import {
observable,
asMap,
asFlat,
action,
asReference,
ObservableMap,
computed,
runInAction
} from 'mobx'
import Collection from './Collection'
import { uniqueId, isString, debounce } from 'lodash'
import apiClient from './apiClient'
Expand Down Expand Up @@ -27,16 +36,47 @@ export default class Model {
}

/**
* Return the url for this given REST resource
* Return the base url used in
* the `url` method
*
* @abstract
*/
urlRoot () {
throw new Error('`url` method not implemented')
}

/**
* Return the url for this given REST resource
*/
url (): string {
let urlRoot

if (this.collection) {
return `${this.collection.url()}/${this.get('id')}`
urlRoot = this.collection.url()
} else {
urlRoot = this.urlRoot()
}

throw new Error('`url` method not implemented')
if (!urlRoot) {
throw new Error('Either implement `urlRoot` or assign a collection')
}

if (this.isNew) {
return urlRoot
} else {
return `${urlRoot}/${this.get('id')}`
}
}

/**
* Wether the resource is new or not
*
* We determine this asking if it contains
* the `id` attribute (set by the server).
*/
@computed
get isNew (): boolean {
return !this.has('id')
}

/**
Expand Down

0 comments on commit 4deec89

Please sign in to comment.