diff --git a/src/base_model.js b/src/base_model.js index d13086e..440a542 100644 --- a/src/base_model.js +++ b/src/base_model.js @@ -1,6 +1,4 @@ -import { - runInAction, isObservableArray -} from 'mobx'; +import { runInAction } from 'mobx'; import { tableize, underscore, camelize } from 'inflection'; import filter from 'lodash/filter'; import uniqueId from 'lodash/uniqueId'; @@ -21,31 +19,28 @@ const initObservables = function(target) { if (!target.observables) { target.observables = { collection: [] }; } -} - - -class BaseModel { +}; +class MobxModel { static attributes = {}; static relations = []; id = null; - lastSetRequestId = null; + lastSetRequestId = null; static get = function(id) { - let items = result(this, 'observables.collection') + let items = result(this, 'observables.collection'); if (items) { let l = items.length; - for(var i = 0; i < l; i++) { + for (var i = 0; i < l; i++) { if (items[i].id.toString() === id.toString()) return items[i]; - } - } + } + } return null; - }; + }; static set = function(options = {}) { - let { modelJson, topLevelJson, requestId } = options; /* @@ -61,13 +56,13 @@ class BaseModel { if (!topLevelJson) topLevelJson = modelJson; let model = this.get(modelJson.id); - + runInAction(() => { if (!model) { model = new this({ modelJson, topLevelJson, - requestId + requestId, }); this.observables.collection.push(model); @@ -81,9 +76,12 @@ class BaseModel { return model; }; - static remove = function (model) { + static remove = function(model) { if (this.observables && this.observables.collection) { - this.observables.collection.splice(this.observables.collection.indexOf(model), 1); + this.observables.collection.splice( + this.observables.collection.indexOf(model), + 1, + ); } }; @@ -96,29 +94,22 @@ class BaseModel { Object.defineProperty(this, actionName, { get: function() { return method.bind(this); - } + }, }); - }; + } static addAction(actionName, method) { Object.defineProperty(this.prototype, actionName, { get: function() { return method.bind(this); - } + }, }); - }; - - constructor(options = {}) { - let { - modelJson, - topLevelJson, - requestId - } = options; - - - initObservables(this.constructor) + } + constructor(options = {}) { + let { modelJson, topLevelJson, requestId } = options; + initObservables(this.constructor); if (modelJson && modelJson.id) { this.id = modelJson.id; @@ -130,7 +121,6 @@ class BaseModel { this.onInitialize(); } - set(options = {}) { let { requestId, modelJson, topLevelJson } = options; let model = this; @@ -145,17 +135,16 @@ class BaseModel { runInAction(() => { setAttributes({ model, modelJson }); - + setRelations({ model, requestId, modelJson, - topLevelJson + topLevelJson, }); }); } - get urlRoot() { return this.constructor.urlRoot; } @@ -164,12 +153,11 @@ class BaseModel { return this.constructor.jsonKey; } - onInitialize() { - } + onInitialize() {} - onDestroy() { + onDestroy() { runInAction(() => { - this.removeSelfFromCollection(); + this.removeSelfFromCollection(); this.destroyDependentRelations(); this.removeSelfFromRelations(); }); @@ -189,22 +177,20 @@ class BaseModel { if (relation.isHasMany) { this[relation.propertyName].slice().forEach(relatedModel => { relatedModel.onDestroy(); - }) + }); } else if (relation.isHasOne) { this[relation.propertyName].onDestroy(); } - }) + }); } removeSelfFromRelations() { - let relationsToRemoveFrom = filter(this.constructor.relations, relation => { let reverseRelation = relation.reverseRelation; return reverseRelation && reverseRelation.onDestroy === 'removeSelf'; }); relationsToRemoveFrom.forEach(relation => { - let removeMethodName = relation.reverseRelation.removeMethodName; if (relation.isHasMany) { @@ -212,40 +198,50 @@ class BaseModel { if (relatedModel[removeMethodName]) { relatedModel[removeMethodName](this); } - }) + }); } else if (relation.isHasOne) { - // console.log(relation.propertyName, removeMethodName, this[relation.propertyName]) - if (this[relation.propertyName] && this[relation.propertyName][removeMethodName]) { + if ( + this[relation.propertyName] && + this[relation.propertyName][removeMethodName] + ) { this[relation.propertyName][removeMethodName](this); } } - }) + }); } - toJSON () { + toJSON() { const { id, constructor } = this; const { attributes, relations } = constructor; // collect attributes - const attributeValues = Object.keys(attributes || {}).reduce((values, attributeName) => { - values[attributeName] = this[attributeName]; - return values; - }, {}); + const attributeValues = Object.keys(attributes || {}).reduce( + (values, attributeName) => { + values[attributeName] = this[attributeName]; + return values; + }, + {}, + ); // collect relation models id - const relationValues = (relations || []).reduce((values, {type, propertyName, foreignKey}) => { - const camelizedForeignKey = camelize(foreignKey, true); - - if (type === 'hasMany') { - values[camelizedForeignKey] = (this[propertyName] || []).slice().map(model => model.id); - } + const relationValues = (relations || []).reduce( + (values, { type, propertyName, foreignKey }) => { + const camelizedForeignKey = camelize(foreignKey, true); + + if (type === 'hasMany') { + values[camelizedForeignKey] = (this[propertyName] || []) + .slice() + .map(model => model.id); + } - if (type === 'hasOne') { - values[camelizedForeignKey] = (this[propertyName] || {}).id; - } + if (type === 'hasOne') { + values[camelizedForeignKey] = (this[propertyName] || {}).id; + } - return values; - }, {}); + return values; + }, + {}, + ); return { id, @@ -253,26 +249,28 @@ class BaseModel { ...relationValues, }; } - } -Object.defineProperty(BaseModel, 'urlRoot', { +Object.defineProperty(MobxModel, 'urlRoot', { get: function() { - return this._urlRoot ? this._urlRoot : '/'+tableize(this.modelName || this.name); + return this._urlRoot + ? this._urlRoot + : '/' + tableize(this.modelName || this.name); }, set: function(value) { this._urlRoot = value; - } + }, }); -Object.defineProperty(BaseModel, 'jsonKey', { +Object.defineProperty(MobxModel, 'jsonKey', { get: function() { - return this._jsonKey ? this._jsonKey : underscore(this.modelName || this.name); + return this._jsonKey + ? this._jsonKey + : underscore(this.modelName || this.name); }, set: function(value) { this._jsonKey = value; - } + }, }); - -export default BaseModel; \ No newline at end of file +export default MobxModel;