Company.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * @Description:
  3. * @Author: hy
  4. * @Date: 2019-08-13 11:39:00
  5. * @LastEditTime: 2019-08-16 15:32:34
  6. */
  7. Ext.define('uas.model.Company', {
  8. extend: 'uas.model.Base',
  9. requires: [
  10. 'uas.model.field.PhoneNumber',
  11. 'Ext.data.proxy.Rest'
  12. ],
  13. fields: [
  14. {name: 'name'},
  15. {name: 'phone', type: 'phonenumber' },
  16. {name: 'price', type: 'float'},
  17. { name: 'priceChange', type: 'float' },
  18. { name: 'priceChangePct', type: 'float' },
  19. { name: 'priceLastChange', type: 'date', dateReadFormat: 'n/j' },
  20. // Calculated field. Depends on price value. Adds it to price history.
  21. // Trend begins with the current price. Changes get pushed onto the end
  22. {
  23. name: 'trend',
  24. calculate: function(data) {
  25. // Avoid circular dependency by hiding the read of trend value
  26. var trend = data['trend'] || (data['trend'] = []);
  27. trend.push(data.price);
  28. if (trend.length === 1) {
  29. //let's start the trend off with a change
  30. trend.push(data.price + data.priceChange);
  31. }
  32. if (trend.length > 10) {
  33. trend.shift();
  34. }
  35. return trend;
  36. },
  37. // It's the same array. But we need Model#set to see it as modified so it
  38. // is flushed to the UI
  39. isEqual: function() {
  40. return false;
  41. }
  42. },
  43. // Calculated field. Depends on price history being populated.
  44. {
  45. name: 'change',
  46. type: 'float',
  47. calculate: function(data) {
  48. var trend = data.trend,
  49. len = trend.length;
  50. return len > 1 ? trend[len - 1] - trend[len - 2] : 0;
  51. }
  52. },
  53. // Calculated field. Depends on price history and last change being populated.
  54. {
  55. name: 'pctChange',
  56. type: 'float',
  57. calculate: function(data) {
  58. var trend = data.trend,
  59. len = trend.length;
  60. return len > 1 ? (data.change / trend[len - 2]) * 100 : 0;
  61. }
  62. },
  63. // Calculated field, recalculated when price changes
  64. {
  65. name: 'lastChange',
  66. type: 'date',
  67. depends: ['price'],
  68. // The calculator is run whenever price changes.
  69. // This field is a purely calculated value and can not be edited.
  70. calculate: function() {
  71. return new Date();
  72. }
  73. },
  74. {name: 'industry'},
  75. {name: 'desc'},
  76. // Rating dependent upon last price change performance 0 = best, 2 = worst
  77. {
  78. name: 'rating',
  79. type: 'int',
  80. // Use a converter to only derive the value onces on record creation
  81. convert: function(value, record) {
  82. var data = record.data,
  83. pct = data.pctChange;
  84. // Only calculate it first time.
  85. if (!data.hasOwnProperty('rating')) {
  86. return (pct < -5) ? 2 : ((pct < 5) ? 1 : 0);
  87. }
  88. return value;
  89. }
  90. }
  91. ],
  92. proxy: {
  93. type: 'ajax',
  94. reader: {
  95. type: 'json'
  96. },
  97. url: '/uas/Company'
  98. },
  99. validators: {
  100. name: 'presence'
  101. },
  102. addPriceTick: function () {
  103. // Set data, but pass "clean" flag.
  104. this.set('price', this.generateNewPrice(), {
  105. dirty: false
  106. });
  107. },
  108. generateNewPrice: function () {
  109. var newPrice = Math.abs(this.data.price + Ext.Number.randomInt(-2345, 2345) / 100);
  110. return Math.round(newPrice * 100) / 100;
  111. }
  112. });