Advertisement
Guest User

Untitled

a guest
Nov 24th, 2015
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Group from "/App/Group";
  2. import Carbon from "/Carbon/Carbon";
  3. import Operator from "/App/Operator";
  4. import Criterion from "/App/Criterion";
  5. import Markdown from "/App/Support/Markdown";
  6. import Collection from "/Support/Collection";
  7. import InternetOption from "/App/InternetOption";
  8. import {default as ActiveRecord} from "/ActiveRecord/Async/Collection";
  9.  
  10. /**
  11.  *
  12.  */
  13. export class Text {
  14.     /**
  15.      * @type {Function}
  16.      */
  17.     text = ko.observable('');
  18.  
  19.     /**
  20.      * @type {Function}
  21.      */
  22.     $preview = null;
  23.  
  24.     /**
  25.      * @type {Function}
  26.      */
  27.     edit = ko.observable(false);
  28.  
  29.     /**
  30.      * @param text
  31.      */
  32.     constructor(text) {
  33.         this.text(text || '');
  34.     }
  35.  
  36.     /**
  37.      * Optimized code: Lazyload (don't touch plz)
  38.      *
  39.      * @returns {Function}
  40.      */
  41.     get preview() {
  42.         if (this.$preview === null) {
  43.             this.$preview = ko.observable('');
  44.             this.text.subscribe(value => {
  45.                 this.$preview(Markdown.parse(value || ''));
  46.             });
  47.         }
  48.  
  49.         this.$preview(Markdown.parse(this.text()));
  50.  
  51.         return this.$preview;
  52.     }
  53.  
  54.     /**
  55.      * @returns {*}
  56.      */
  57.     toString() {
  58.         return this.text();
  59.     }
  60. }
  61.  
  62. /**
  63.  * @property {Carbon} created_at
  64.  * @property {Carbon} updated_at
  65.  */
  66. export default class Tariff extends ActiveRecord {
  67.     /**
  68.      * @type {{index: string, get: string, save: string}}
  69.      */
  70.     static routes = {
  71.         index: '/tariffs.json',
  72.         get:   '/tariff/{id}.json',
  73.         save:  '/tariff/save.json'
  74.     };
  75.  
  76.     /**
  77.      * @constructor static
  78.      */
  79.     static constructor() {
  80.         this.on('creating', (data) => {
  81.             data.created_at              = Carbon.parse(data.created_at);
  82.             data.updated_at              = Carbon.parse(data.updated_at);
  83.  
  84.             // Cost
  85.             data.has_direct_cost         = ko.observable(data.has_direct_cost);
  86.             data.is_internet             = ko.observable(data.is_internet);
  87.             data.platform_type           = ko.observable(data.platform_type);
  88.  
  89.             // Texts
  90.             data.description             = new Text(data.description);
  91.             data.description_short       = new Text(data.description_short);
  92.             data.description_advantages  = new Text(data.description_advantages);
  93.             data.description_limitations = new Text(data.description_limitations);
  94.  
  95.  
  96.             if (typeof data.tags === 'string') {
  97.                 data.tags = JSON.parse(data.tags);
  98.             } else if (!(data.tags instanceof Array)) {
  99.                 data.tags = [data.tags];
  100.             }
  101.  
  102.             if (data.criteria) {
  103.                 data.criteria = (new Collection(data.criteria || []))
  104.                     .map(attributes => {
  105.                         return Criterion.create(attributes);
  106.                     });
  107.             }
  108.  
  109.             if (data.internet_options) {
  110.                 data.internet_options = (new Collection(data.internet_options || []))
  111.                     .map(attributes => {
  112.                         return InternetOption.create(attributes);
  113.                     });
  114.             }
  115.         });
  116.  
  117.         this.on('saving', (data) => {
  118.  
  119.             var result = {};
  120.             for (var key in data) {
  121.                 var value = data[key];
  122.  
  123.                 if (value instanceof Carbon) {
  124.                     result[key] = value.toIso8601String();
  125.  
  126.                 } else if (value instanceof Collection) {
  127.                     result[key] = value.map(inner => {
  128.                         if (inner instanceof InternetOption) {
  129.                             return inner.toObject();
  130.                         }
  131.                         return {};
  132.                     }).toArray();
  133.  
  134.                 } else if (value instanceof Text) {
  135.                     result[key] = value.text();
  136.  
  137.                 } else if (value instanceof Function) {
  138.                     result[key] = value();
  139.  
  140.                 } else {
  141.                     result[key] = value;
  142.                 }
  143.             }
  144.  
  145.             console.log(result);
  146.  
  147.             return result;
  148.         });
  149.     }
  150.  
  151.     /**
  152.      * @returns {Model}
  153.      */
  154.     static async load() {
  155.         await super.load({title: 'Загрузка тарифов'});
  156.         await Operator.load();
  157.  
  158.         return this;
  159.     }
  160.  
  161.     /**
  162.      * @returns {Collection}
  163.      */
  164.     get group() {
  165.         return this.hasOne(Group, 'group_id', 'id');
  166.     }
  167.  
  168.     /**
  169.      * @returns {Collection}
  170.      */
  171.     get operator() {
  172.         return this.hasOne(Operator, 'operator_id', 'id');
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement