Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- submit: function () {
- var self = this;
- var division = this.get('model');
- var isNew = division.get('isNew');
- if(isNew) {
- division.setProperties({
- vendor: self.get('parentVendor'),
- vendorCategory: self.get('selectedCategory'),
- isActive: true,
- createdBy: 1,
- createdDate: moment().format()
- });
- } else {
- division.setProperties({
- vendorCategory: self.get('selectedCategory'),
- modifiedBy: 1,
- modifiedDate: moment().format()
- });
- }
- division.validate().then(function () {
- if (division.get('isValid')) {
- division.save().then(function(result) {
- /*
- this bit of hackishness loops through each of the attribute input fields, creates a new DS.model for that attribute,
- saves the model, then returns the resulting promise to the promises array so they can be processed by RSVP.all to
- ensure that all the attributes save properly.
- */
- // map the promises array
- var promises = $('input.attribute').map(function () {
- var elem = $(this)[0];
- var vendorTypeAttrId = $(elem).attr('data-attribute-id'),
- attrValue = $(elem).val();
- // create the model
- var model = self.store.createRecord('vendor-division-attribute', {
- division: result,
- vendorTypeAttribute: self.get('categoryAttributes').findBy('id', vendorTypeAttrId),
- value: attrValue,
- createdBy: 1,
- createdDate: moment().format()
- });
- // return the save promise
- var p = model.save();
- return p;
- });
- // Make sure all promises are resolved before continuing
- Ember.RSVP.all(promises).then(function (results) {
- // it worked! keep calm and move along
- self.send('closeModal');
- var msg = isNew ? division.get('name') + ' Created!' : 'Changes saved!';
- Bootstrap.NM.push(msg, 'success');
- }).catch(function(reason) {
- // something went wrong - display the error
- if (typeof reason.error !== 'undefined') {
- // DSP error
- if (Ember.keys(reason.error).length > 0) {
- error = reason.error[0].message;
- } else {
- error = 'An error occured while saving your changes';
- }
- } else if (typeof reason.message !== 'undefined') {
- error = reason.message;
- } else {
- error = reason;
- }
- console.log(error);
- Bootstrap.NM.push('Error saving Vendor Division: ' + error, 'danger');
- });
- }, function(reason) {
- var error;
- if (typeof reason.error !== 'undefined') {
- // DSP error
- if (Ember.keys(reason.error).length > 0) {
- error = reason.error[0].message;
- } else {
- error = 'An error occured while saving your changes';
- }
- } else if (typeof reason.message !== 'undefined') {
- error = reason.message;
- } else {
- error = reason;
- }
- console.log(error);
- Bootstrap.NM.push('Error saving Vendor Division: ' + error, 'danger');
- });
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement