Skip to content

Commit

Permalink
Code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Staltec committed Aug 9, 2019
1 parent d5c8b8c commit 97540ad
Showing 1 changed file with 71 additions and 73 deletions.
144 changes: 71 additions & 73 deletions src/base_model.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;

/*
Expand All @@ -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);
Expand All @@ -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,
);
}
};

Expand All @@ -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;
Expand All @@ -130,7 +121,6 @@ class BaseModel {
this.onInitialize();
}


set(options = {}) {
let { requestId, modelJson, topLevelJson } = options;
let model = this;
Expand All @@ -145,17 +135,16 @@ class BaseModel {

runInAction(() => {
setAttributes({ model, modelJson });

setRelations({
model,
requestId,
modelJson,
topLevelJson
topLevelJson,
});
});
}


get urlRoot() {
return this.constructor.urlRoot;
}
Expand All @@ -164,12 +153,11 @@ class BaseModel {
return this.constructor.jsonKey;
}

onInitialize() {
}
onInitialize() {}

onDestroy() {
onDestroy() {
runInAction(() => {
this.removeSelfFromCollection();
this.removeSelfFromCollection();
this.destroyDependentRelations();
this.removeSelfFromRelations();
});
Expand All @@ -189,90 +177,100 @@ 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) {
this[relation.propertyName].slice().forEach(relatedModel => {
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,
...attributeValues,
...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;
export default MobxModel;

0 comments on commit 97540ad

Please sign in to comment.