Changes in Sidecar model not reflected in Sugar bean

I have a requirement where a field called resource_rate_actual has be set equal to one of several fields, depending on the value selected in a dropdown. I've set it up in record.js view controller like this:

initialize: function (options) {
        this._super('initialize', [options]);
        this.model.on('change:resource_type', this.resource_type_changed, this);
    }, 
resource_type_changed: function(){
        if(!_.isUndefined(this.model.get('resource_type'))) {
            switch (this.model.get('resource_type')) {
                case 'Project Manager':
                    this.model.set('resource_rate_actual', this.model.get('project_manager_rate_c'));
                    break;
                case 'Engineer':
                    this.model.set('resource_rate_actual', this.model.get('engineer_rate_c'));
                    break;
            }
        }
    },

This works well to automatically set the resource_rate_actual field on the front end, depending on whether a Project Manager or an Engineer is selected in the dropdown. But, when the record was saved, the value in the resource_rate_actual field did not get saved to the database. In order to save it, I had to set up a before_save logic hook to set the value of $bean->resource_rate_actual on the back end with this code:

switch ($bean->custom_rate) {
                case 'Project Manager':
                    $bean->resource_rate_actual = $bean->project_manager_rate_c;
                break;
                case 'Project Manager':
                    $bean->resource_rate_actual = $bean->project_manager_rate_c;
                    break;

}

With this logic hook, the record was saved as displayed on the front end.

My question is, why is it necessary to set the value of resource_rate_actual both on the front end and on the back end? As I understand it, a Sidecar model is a JS object that represents the Sugar Bean, and any changes to the model should be reflected in the bean. Is this not correct?

Parents
  • Hi Yury Voloshin,

    You are right that any changes to the model are reflected in the bean. I never used logic hooks to manage my frontend and backend separately. I guess the issue is maybe this.model.set is executing before this.model.get. Well, it also should not happen because inner code executes first.

    Please try this:

    var pro_manager = this.model.get('project_manager_rate_c');

    this.model.set('resource_rate_actual', pro_manager);

     

    One more thing is that if you are setting these values for the creation of the new record then add the same code for the CreateView as well in create.js file. Otherwise, it will only work just for the edit/save not for the create/save.

     

     

  • Thank you Maryam! Breaking up this.model.set and this.model.get into two statements instead of one actually worked and now the record is saved with the logic hook deactivated. This is strange because having them as one statement works well in other view controllers. But in any case, now I'm able to remove the logic hook.  

Reply Children
No Data