Company.js 3.7 KB

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