Comparing version 1.0.2 to 1.0.3
@@ -18,9 +18,4 @@ import { Collection, Model, apiClient } from '../src' | ||
class MyModel extends Model { | ||
url () { | ||
const id = this.has('id') && this.get('id') | ||
if (id) { | ||
return `/resources/${id}` | ||
} else { | ||
return '/resources' | ||
} | ||
urlRoot () { | ||
return '/resources' | ||
} | ||
@@ -50,12 +45,42 @@ } | ||
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') | ||
}) | ||
}) | ||
}) | ||
@@ -62,0 +87,0 @@ }) |
@@ -90,3 +90,4 @@ 'use strict'; | ||
/** | ||
* Return the url for this given REST resource | ||
* Return the base url used in | ||
* the `url` method | ||
* | ||
@@ -98,12 +99,45 @@ * @abstract | ||
_createClass(Model, [{ | ||
key: 'urlRoot', | ||
value: function urlRoot() { | ||
throw new Error('`url` method not implemented'); | ||
} | ||
/** | ||
* Return the url for this given REST resource | ||
*/ | ||
}, { | ||
key: 'url', | ||
value: function url() { | ||
var urlRoot = void 0; | ||
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). | ||
*/ | ||
}, { | ||
key: 'get', | ||
/** | ||
* Get the attribute from the model. | ||
@@ -120,5 +154,2 @@ * | ||
*/ | ||
}, { | ||
key: 'get', | ||
value: function get(attribute) { | ||
@@ -591,2 +622,7 @@ if (this.has(attribute)) { | ||
}, { | ||
key: 'isNew', | ||
get: function get() { | ||
return !this.has('id'); | ||
} | ||
}, { | ||
key: 'id', | ||
@@ -615,3 +651,3 @@ get: function get() { | ||
} | ||
}), _applyDecoratedDescriptor(_class.prototype, 'set', [_mobx.action], Object.getOwnPropertyDescriptor(_class.prototype, 'set'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'fetch', [_mobx.action], Object.getOwnPropertyDescriptor(_class.prototype, 'fetch'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'save', [_mobx.action], Object.getOwnPropertyDescriptor(_class.prototype, 'save'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'destroy', [_mobx.action], Object.getOwnPropertyDescriptor(_class.prototype, 'destroy'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'rpc', [_mobx.action], Object.getOwnPropertyDescriptor(_class.prototype, 'rpc'), _class.prototype)), _class); | ||
}), _applyDecoratedDescriptor(_class.prototype, 'isNew', [_mobx.computed], Object.getOwnPropertyDescriptor(_class.prototype, 'isNew'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'set', [_mobx.action], Object.getOwnPropertyDescriptor(_class.prototype, 'set'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'fetch', [_mobx.action], Object.getOwnPropertyDescriptor(_class.prototype, 'fetch'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'save', [_mobx.action], Object.getOwnPropertyDescriptor(_class.prototype, 'save'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'destroy', [_mobx.action], Object.getOwnPropertyDescriptor(_class.prototype, 'destroy'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'rpc', [_mobx.action], Object.getOwnPropertyDescriptor(_class.prototype, 'rpc'), _class.prototype)), _class); | ||
exports.default = Model; |
{ | ||
"name": "mobx-rest", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "REST conventions for mobx.", | ||
@@ -5,0 +5,0 @@ "repository": { |
66702
1660