Employee.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. Ext.define('uas.model.Employee', {
  2. extend: 'uas.model.Base',
  3. fields: [{
  4. name: 'employeeNo'
  5. }, {
  6. name: 'rating'
  7. }, {
  8. name: 'salary',
  9. type: 'float'
  10. }, {
  11. name: 'forename'
  12. }, {
  13. name: 'surname'
  14. }, {
  15. name: 'name',
  16. convert: function(v, rec) {
  17. return rec.editing ? v : rec.get('forename') + ' ' + rec.get('surname');
  18. }
  19. }, {
  20. name: 'email'
  21. }, {
  22. name: 'department'
  23. }, {
  24. name: 'dob',
  25. type: 'date',
  26. dateFormat: 'Ymd'
  27. }, {
  28. name: 'joinDate',
  29. type: 'date',
  30. dateFormat: 'Ymd'
  31. }, {
  32. name: 'noticePeriod'
  33. }, {
  34. name: 'sickDays',
  35. type: 'int'
  36. }, {
  37. name: 'holidayDays',
  38. type: 'int'
  39. }, {
  40. name: 'holidayAllowance',
  41. type: 'int'
  42. }, {
  43. name: 'avatar'
  44. }, {
  45. name: 'ratingLastYear',
  46. type: 'int'
  47. }, {
  48. name: 'ratingThisYear',
  49. type: 'int'
  50. }, {
  51. name: 'active',
  52. type: 'bool',
  53. persist: false,
  54. calculate: function() {
  55. return !!(Math.random() >= 0.5);
  56. }
  57. }],
  58. idField: 'employeeNo',
  59. // Override set to update dependent fields
  60. set: function (name, value) {
  61. var data = name;
  62. // Convert 2 arg form to object form
  63. if (Ext.isString(name)) {
  64. data = {};
  65. data[name] = value;
  66. }
  67. // "name" is a calculated field, so update it on edit of "forename" or "surname".
  68. if (data.forename || data.surname) {
  69. data.name = (data.forename || this.get('forename')) + ' ' + (data.surname || this.get('surname'));
  70. }
  71. // Likewise, update two name fields if whole name gets updated
  72. else if (data.name) {
  73. var names = this.convertName(data.name);
  74. data.forename = names[0];
  75. data.surname = names[1];
  76. }
  77. return this.callParent([data]);
  78. },
  79. convertName: function(name) {
  80. var names = /([^\s+]+)(?:\s+(.*))?/.exec(name);
  81. return names ? [names[1], names[2]||''] : ['', ''];
  82. }
  83. });