Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Group from "/App/Group";
- import Carbon from "/Carbon/Carbon";
- import Operator from "/App/Operator";
- import Criterion from "/App/Criterion";
- import Markdown from "/App/Support/Markdown";
- import Collection from "/Support/Collection";
- import InternetOption from "/App/InternetOption";
- import {default as ActiveRecord} from "/ActiveRecord/Async/Collection";
- /**
- *
- */
- export class Text {
- /**
- * @type {Function}
- */
- text = ko.observable('');
- /**
- * @type {Function}
- */
- $preview = null;
- /**
- * @type {Function}
- */
- edit = ko.observable(false);
- /**
- * @param text
- */
- constructor(text) {
- this.text(text || '');
- }
- /**
- * Optimized code: Lazyload (don't touch plz)
- *
- * @returns {Function}
- */
- get preview() {
- if (this.$preview === null) {
- this.$preview = ko.observable('');
- this.text.subscribe(value => {
- this.$preview(Markdown.parse(value || ''));
- });
- }
- this.$preview(Markdown.parse(this.text()));
- return this.$preview;
- }
- /**
- * @returns {*}
- */
- toString() {
- return this.text();
- }
- }
- /**
- * @property {Carbon} created_at
- * @property {Carbon} updated_at
- */
- export default class Tariff extends ActiveRecord {
- /**
- * @type {{index: string, get: string, save: string}}
- */
- static routes = {
- index: '/tariffs.json',
- get: '/tariff/{id}.json',
- save: '/tariff/save.json'
- };
- /**
- * @constructor static
- */
- static constructor() {
- this.on('creating', (data) => {
- data.created_at = Carbon.parse(data.created_at);
- data.updated_at = Carbon.parse(data.updated_at);
- // Cost
- data.has_direct_cost = ko.observable(data.has_direct_cost);
- data.is_internet = ko.observable(data.is_internet);
- data.platform_type = ko.observable(data.platform_type);
- // Texts
- data.description = new Text(data.description);
- data.description_short = new Text(data.description_short);
- data.description_advantages = new Text(data.description_advantages);
- data.description_limitations = new Text(data.description_limitations);
- if (typeof data.tags === 'string') {
- data.tags = JSON.parse(data.tags);
- } else if (!(data.tags instanceof Array)) {
- data.tags = [data.tags];
- }
- if (data.criteria) {
- data.criteria = (new Collection(data.criteria || []))
- .map(attributes => {
- return Criterion.create(attributes);
- });
- }
- if (data.internet_options) {
- data.internet_options = (new Collection(data.internet_options || []))
- .map(attributes => {
- return InternetOption.create(attributes);
- });
- }
- });
- this.on('saving', (data) => {
- var result = {};
- for (var key in data) {
- var value = data[key];
- if (value instanceof Carbon) {
- result[key] = value.toIso8601String();
- } else if (value instanceof Collection) {
- result[key] = value.map(inner => {
- if (inner instanceof InternetOption) {
- return inner.toObject();
- }
- return {};
- }).toArray();
- } else if (value instanceof Text) {
- result[key] = value.text();
- } else if (value instanceof Function) {
- result[key] = value();
- } else {
- result[key] = value;
- }
- }
- console.log(result);
- return result;
- });
- }
- /**
- * @returns {Model}
- */
- static async load() {
- await super.load({title: 'Загрузка тарифов'});
- await Operator.load();
- return this;
- }
- /**
- * @returns {Collection}
- */
- get group() {
- return this.hasOne(Group, 'group_id', 'id');
- }
- /**
- * @returns {Collection}
- */
- get operator() {
- return this.hasOne(Operator, 'operator_id', 'id');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement