Browse Source

Move JsonGenerator to TS

pull/4746/head
Artur Arseniev 4 years ago
parent
commit
889dd234c7
  1. 38
      src/code_manager/model/JsonGenerator.js
  2. 39
      src/code_manager/model/JsonGenerator.ts

38
src/code_manager/model/JsonGenerator.js

@ -1,38 +0,0 @@
import { each } from 'underscore';
import { Model, Collection } from '../../common';
export default class JsonGenerator extends Model {
build(model) {
var json = model.toJSON();
this.beforeEach(json);
each(
json,
function (v, attr) {
var obj = json[attr];
if (obj instanceof Model) {
json[attr] = this.build(obj);
} else if (obj instanceof Collection) {
var coll = obj;
json[attr] = [];
if (coll.length) {
coll.each(function (el, index) {
json[attr][index] = this.build(el);
}, this);
}
}
},
this
);
return json;
}
/**
* Execute on each object
* @param {Object} obj
*/
beforeEach(obj) {
delete obj.status;
}
}

39
src/code_manager/model/JsonGenerator.ts

@ -0,0 +1,39 @@
import { each } from 'underscore';
import { Model, Collection } from '../../common';
import Component from '../../dom_components/model/Component';
type ComponentProps = Record<string, any>;
export default class JsonGenerator extends Model {
build(model: Component) {
// @ts-ignore
const json = model.toJSON() as ComponentProps;
this.beforeEach(json);
each(json, (v, attr) => {
const obj = json[attr];
if (obj instanceof Model) {
// @ts-ignore
json[attr] = this.build(obj);
} else if (obj instanceof Collection) {
const coll = obj;
json[attr] = [];
if (coll.length) {
coll.forEach((el, index) => {
json[attr][index] = this.build(el);
});
}
}
});
return json;
}
/**
* Execute on each object
* @param {Object} obj
*/
beforeEach(obj: ComponentProps) {
delete obj.status;
}
}
Loading…
Cancel
Save