Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. submit: function () {
  2. var self = this;
  3. var division = this.get('model');
  4. var isNew = division.get('isNew');
  5.  
  6. if(isNew) {
  7. division.setProperties({
  8. vendor: self.get('parentVendor'),
  9. vendorCategory: self.get('selectedCategory'),
  10. isActive: true,
  11. createdBy: 1,
  12. createdDate: moment().format()
  13. });
  14. } else {
  15. division.setProperties({
  16. vendorCategory: self.get('selectedCategory'),
  17. modifiedBy: 1,
  18. modifiedDate: moment().format()
  19. });
  20. }
  21.  
  22. division.validate().then(function () {
  23. if (division.get('isValid')) {
  24. division.save().then(function(result) {
  25.  
  26. /*
  27. this bit of hackishness loops through each of the attribute input fields, creates a new DS.model for that attribute,
  28. saves the model, then returns the resulting promise to the promises array so they can be processed by RSVP.all to
  29. ensure that all the attributes save properly.
  30. */
  31.  
  32. // map the promises array
  33. var promises = $('input.attribute').map(function () {
  34. var elem = $(this)[0];
  35. var vendorTypeAttrId = $(elem).attr('data-attribute-id'),
  36. attrValue = $(elem).val();
  37.  
  38. // create the model
  39. var model = self.store.createRecord('vendor-division-attribute', {
  40. division: result,
  41. vendorTypeAttribute: self.get('categoryAttributes').findBy('id', vendorTypeAttrId),
  42. value: attrValue,
  43. createdBy: 1,
  44. createdDate: moment().format()
  45. });
  46.  
  47. // return the save promise
  48. var p = model.save();
  49. return p;
  50. });
  51.  
  52. // Make sure all promises are resolved before continuing
  53. Ember.RSVP.all(promises).then(function (results) {
  54. // it worked! keep calm and move along
  55. self.send('closeModal');
  56. var msg = isNew ? division.get('name') + ' Created!' : 'Changes saved!';
  57. Bootstrap.NM.push(msg, 'success');
  58. }).catch(function(reason) {
  59. // something went wrong - display the error
  60. if (typeof reason.error !== 'undefined') {
  61. // DSP error
  62. if (Ember.keys(reason.error).length > 0) {
  63. error = reason.error[0].message;
  64. } else {
  65. error = 'An error occured while saving your changes';
  66. }
  67. } else if (typeof reason.message !== 'undefined') {
  68. error = reason.message;
  69. } else {
  70. error = reason;
  71. }
  72. console.log(error);
  73. Bootstrap.NM.push('Error saving Vendor Division: ' + error, 'danger');
  74. });
  75.  
  76. }, function(reason) {
  77. var error;
  78. if (typeof reason.error !== 'undefined') {
  79. // DSP error
  80. if (Ember.keys(reason.error).length > 0) {
  81. error = reason.error[0].message;
  82. } else {
  83. error = 'An error occured while saving your changes';
  84. }
  85. } else if (typeof reason.message !== 'undefined') {
  86. error = reason.message;
  87. } else {
  88. error = reason;
  89. }
  90. console.log(error);
  91. Bootstrap.NM.push('Error saving Vendor Division: ' + error, 'danger');
  92. });
  93. }
  94. });
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement