From ca168fad93faeac7f8080c3db34d87e47c23d76b Mon Sep 17 00:00:00 2001 From: Kees Kluskens Date: Tue, 18 Apr 2017 16:55:31 +0200 Subject: [PATCH] 0.9.0 --- dist/mobx-spine.es.js | 35 ++++++++++++++++++++++++++++------- dist/mobx-spine.umd.js | 35 ++++++++++++++++++++++++++++------- package.json | 2 +- 3 files changed, 57 insertions(+), 15 deletions(-) diff --git a/dist/mobx-spine.es.js b/dist/mobx-spine.es.js index 95286b5..539978c 100644 --- a/dist/mobx-spine.es.js +++ b/dist/mobx-spine.es.js @@ -37,6 +37,8 @@ var _descriptor$1; var _descriptor2$1; var _descriptor3; var _descriptor4; +var _class2$1; +var _temp$1; function _initDefineProp$1(target, property, descriptor, context) { if (!descriptor) return; @@ -87,16 +89,21 @@ function _applyDecoratedDescriptor$1( const AVAILABLE_CONST_OPTIONS = ['relations', 'limit']; -let Store = ((_class$1 = class Store { - // Holds the fetch parameters +let Store = ((_class$1 = ((_temp$1 = _class2$1 = class Store { get isLoading() { return this.__pendingRequestCount > 0; } - + // Holds the fetch parameters get length() { return this.models.length; } + set backendResourceName(v) { + throw new Error( + '`backendResourceName` should be a static property on the store.' + ); + } + constructor(options = {}) { _initDefineProp$1(this, 'models', _descriptor$1, this); @@ -373,7 +380,8 @@ let Store = ((_class$1 = class Store { } return this.models[index]; } -}), ((_descriptor$1 = _applyDecoratedDescriptor$1( +}), (_class2$1.backendResourceName = + ''), _temp$1)), ((_descriptor$1 = _applyDecoratedDescriptor$1( _class$1.prototype, 'models', [observable], @@ -574,6 +582,8 @@ function generateNegativeId() { let Model = ((_class = ((_temp = _class2 = class Model { // Holds activated - nested - relations (e.g. `['animal', 'animal.breed']`) + + // How the model is known at the backend. This is useful when the model is in a relation that has a different name. get url() { const id = this[this.constructor.primaryKey]; return `${this.urlRoot}${id ? `${id}/` : ''}`; @@ -597,6 +607,12 @@ let Model = ((_class = ((_temp = _class2 = class Model { ); } + set backendResourceName(v) { + throw new Error( + '`backendResourceName` should be a static property on the model.' + ); + } + casts() { return {}; } @@ -723,7 +739,12 @@ let Model = ((_class = ((_temp = _class2 = class Model { data[relBackendName] = myNewId; } const relBackendData = rel.toBackendAll(myNewId); - relations[relBackendName] = relBackendData.data; + // Sometimes the backend knows the relation by a different name, e.g. the relation is called + // `activities`, but the name in the backend is `activity`. + // In that case, you can add `static backendResourceName = 'activity';` to that model. + const realBackendName = + rel.constructor.backendResourceName || relBackendName; + relations[realBackendName] = relBackendData.data; forIn(relBackendData.relations, (relB, key) => { relations[key] = relations[key] ? relations[key].concat(relB) @@ -974,8 +995,8 @@ let Model = ((_class = ((_temp = _class2 = class Model { this[currentRel].clear(); }); } -}), (_class2.primaryKey = - 'id'), _temp)), ((_descriptor = _applyDecoratedDescriptor( +}), (_class2.primaryKey = 'id'), (_class2.backendResourceName = + ''), _temp)), ((_descriptor = _applyDecoratedDescriptor( _class.prototype, '__backendValidationErrors', [observable], diff --git a/dist/mobx-spine.umd.js b/dist/mobx-spine.umd.js index 95da778..2f93f54 100644 --- a/dist/mobx-spine.umd.js +++ b/dist/mobx-spine.umd.js @@ -38,6 +38,8 @@ var _descriptor2$1; var _descriptor3; var _descriptor4; + var _class2$1; + var _temp$1; function _initDefineProp$1(target, property, descriptor, context) { if (!descriptor) return; @@ -90,16 +92,21 @@ const AVAILABLE_CONST_OPTIONS = ['relations', 'limit']; - let Store = ((_class$1 = class Store { - // Holds the fetch parameters + let Store = ((_class$1 = ((_temp$1 = _class2$1 = class Store { get isLoading() { return this.__pendingRequestCount > 0; } - + // Holds the fetch parameters get length() { return this.models.length; } + set backendResourceName(v) { + throw new Error( + '`backendResourceName` should be a static property on the store.' + ); + } + constructor(options = {}) { _initDefineProp$1(this, 'models', _descriptor$1, this); @@ -387,7 +394,8 @@ } return this.models[index]; } - }), ((_descriptor$1 = _applyDecoratedDescriptor$1( + }), (_class2$1.backendResourceName = + ''), _temp$1)), ((_descriptor$1 = _applyDecoratedDescriptor$1( _class$1.prototype, 'models', [mobx.observable], @@ -590,6 +598,8 @@ let Model = ((_class = ((_temp = _class2 = class Model { // Holds activated - nested - relations (e.g. `['animal', 'animal.breed']`) + + // How the model is known at the backend. This is useful when the model is in a relation that has a different name. get url() { const id = this[this.constructor.primaryKey]; return `${this.urlRoot}${id ? `${id}/` : ''}`; @@ -613,6 +623,12 @@ ); } + set backendResourceName(v) { + throw new Error( + '`backendResourceName` should be a static property on the model.' + ); + } + casts() { return {}; } @@ -749,7 +765,12 @@ data[relBackendName] = myNewId; } const relBackendData = rel.toBackendAll(myNewId); - relations[relBackendName] = relBackendData.data; + // Sometimes the backend knows the relation by a different name, e.g. the relation is called + // `activities`, but the name in the backend is `activity`. + // In that case, you can add `static backendResourceName = 'activity';` to that model. + const realBackendName = + rel.constructor.backendResourceName || relBackendName; + relations[realBackendName] = relBackendData.data; lodash.forIn(relBackendData.relations, (relB, key) => { relations[key] = relations[key] ? relations[key].concat(relB) @@ -1010,8 +1031,8 @@ this[currentRel].clear(); }); } - }), (_class2.primaryKey = - 'id'), _temp)), ((_descriptor = _applyDecoratedDescriptor( + }), (_class2.primaryKey = 'id'), (_class2.backendResourceName = + ''), _temp)), ((_descriptor = _applyDecoratedDescriptor( _class.prototype, '__backendValidationErrors', [mobx.observable], diff --git a/package.json b/package.json index db4e1ff..cc80166 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mobx-spine", - "version": "0.8.0", + "version": "0.9.0", "license": "ISC", "author": "Kees Kluskens ", "description": "MobX with support for models, relations and an API.",