How to save parentView().model (parentModel)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rabii
    Active Community Member
    • Jun 2016
    • 1250

    How to save parentView().model (parentModel)

    Hey guys,

    I have a custom rowActions for a relationship which calculate some fields but also set a value on parentView().model i could set the values on parentModel (parentView().mode) but i couldn't save the data on parentView().model: how can i save the parentView().model data from a child relationship custom rowActions below my code:

    Code:
    actionSetHoldingPaid: function (data) {
    var id = data.id;
    if (!id) {
     return;
    }
    var model = this.collection.get(id);
    var parentModel = this.getParentView().model;
    if (!model) {
     return;
    }
    
    model.set({
      holdingStatus: 'Paid',
      totalStatus: 'Partially Paid',
    });
    
    // Set netSuccessFee on parentView model works fine
    parentModel.set('netSuccessFee',
       parentModel.get('netSuccessFee') - model.get('holdingFee')
    );
    
    this.listenToOnce(model, 'sync', function () {
       this.notify(false);
       this.collection.fetch();
    }, this);
    
    this.notify('Saving...');
    // Save parentModel data is not working
    parentModel.save();​
    model.save();
    },​
    Thanks

    telecastg item emillod
    Last edited by rabii; 12-14-2022, 01:36 PM.
    Rabii
    Web Dev
  • item
    Active Community Member
    • Mar 2017
    • 1476

    #2
    Hi rabii,
    i let telecastg or emillod because it's not my skill.

    maybe something so ?

    parentModel.save({status: 'Held'});
    If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

    Comment


    • rabii
      rabii commented
      Editing a comment
      thanks item i have tried parentModel.save({status: 'Held'}); but not worked.
  • item
    Active Community Member
    • Mar 2017
    • 1476

    #3
    I think this do the job. (maybe)
    as you can see, it's provided by telecastg

    Payroll one2many PayrollItem
    when create PayrollItem from bottom panel, some data are retreived from Payroll.
    So i think you just need to do somethink like : payrollObject->save();

    PHP Code:
    define('custom:payrollItem-dynamic-handler', ['dynamic-handler'], function (Dep) {
    
        return Dep.extend({
    
            init: function () {
                /
                this.controlFields();
            },
    
            controlFields: function () {
    
                //console.log(this);
                if(this.recordView.isNew) {
    
                    var payrollId = this.model.attributes.payrollId;
                    //console.log(payrollId)
                    var self = this;
    
                    this.recordView.getCollectionFactory().create('Payroll', function (payrollCollection){
                        payrollCollection.where = [
                            { type:'equals', field:'id', value: payrollId }
                        ];
                        // retrieve the data from the database
                        payrollCollection.fetch().then(function(){
    
                            var  payrollObject = payrollCollection.get(payrollId);
    
                            self.model.set('laborDate',payrollObject.attributes.laborDate);
                            self.model.set('executionDate',payrollObject.attributes.executionDate);
                            self.model.set('practrice',payrollObject.attributes.practrice);
                            self.model.set('isManager',payrollObject.attributes.isManager);
                            self.model.set('name',payrollObject.attributes.name);
                            self.model.set('nationalNumber',payrollObject.attributes.nationalNumber);
                            self.model.set('periodeName',payrollObject.attributes.periodeName);
                            self.model.set('periodeId',payrollObject.attributes.periodeId);
    
    
                        });
                    });
                }
            }
        });
    });
    Last edited by item; 12-15-2022, 07:56 PM.
    If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

    Comment


    • rabii
      rabii commented
      Editing a comment
      Thanks @item
      i have tried parentModel->save(); didn't work. still trying to figure out how it could be done.
  • item
    Active Community Member
    • Mar 2017
    • 1476

    #4
    I have ask to IA chatGPT .. here is response


    If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

    Comment


    • rabii
      rabii commented
      Editing a comment
      Thanks i already get the parent model using the code below, i figure out why it was not working though i had the field to be calculated as a read only and that is why the front-end couldn't save the model, i have changed the field to not be ready only and everything works fine. i am still confused why this happens.

      This code works fine now :
      Code:
      actionSetHoldingPaid: function (data) {
      var id = data.id;
      if (!id) {
       return;
      }
      var model = this.collection.get(id);
      var parentModel = this.getParentView().model;
      if (!model) {
       return;
      }
      
      model.set({
        holdingStatus: 'Paid',
        totalStatus: 'Partially Paid',
      });
      
      // Set netSuccessFee on parentView model works fine
      parentModel.set('netSuccessFee',
         parentModel.get('netSuccessFee') - model.get('holdingFee')
      );
      
      this.listenToOnce(model, 'sync', function () {
         this.notify(false);
         this.collection.fetch();
      }, this);
      
      this.notify('Saving...');
      // Save parentModel data is not working
      parentModel.save();​
      model.save();
      },​
      Thanks for your help.
Working...