Dynamic-handler, onChange Duration time

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • krzysztofpro
    Junior Member
    • Jan 2017
    • 5

    Dynamic-handler, onChange Duration time

    Hi! I try to understand dynamic-handlers and create it on Meeting entity. Have field with price per hour (on linked parent Client account) and want calculate it on fly when duration time's change. Anyone can explain this, or repair code?

    Code:
    define('custom:meetingA-dynamic-handler', ['dynamic-handler'], function (Dep) {
        return Dep.extend({
            init: function () {
                this.controlFields();
                
                //Ustawienie wartości
                this.listenTo(this.model, 'change:parentId', () => {
                    console.log('parentId changedV1');
                    
                    if (this.model.get('parentId') && this.model.get('parentType') == 'Account' && this.model.hasChanged('parentId')) {
                        console.log('parentId changedV2');
                        
                        this.ajaxGetRequest('Account/' + this.model.get('parentId')).then(function (Klient) {
                            if (Klient['klientStawkaGodzinowaWaluta'] != null) {
                                if (typeof Klient['klientStawkaGodzinowaWaluta'] === 'number' && !isNaN(this.model.get('duration'))) {
                                    var koszt = ((Klient['klientStawkaGodzinowaWaluta']) / 3600) * this.model.get('duration');
                                    this.model.set('spotkanieKosztWaluta', Math.round(koszt * 100) / 100);
                                } else {
                                    // Obsługa błędu lub ustawienie domyślnej wartości
                                };
    
                                this.model.set('rodzajRozliczenia', 'godzinowe');                    
                                
                            } else {
                                //Espo.Ui.success('Some error message.', true);
                                //Espo.Ui.warning('Some error message.', true);
                                Espo.Ui.error('Brak stawki godzinowej na kartotece klienta!', true);
                                
                                /*
                                this.confirm({
                                    message: this.translate('Brak stawki godzinowej w kartotece klienta!', 'messages'),
                                    confirmText: this.translate('Potwierdź'), // text of the confirmation button
                                }).then(() => {
                                    // here do some actions
                                });
                                */
                            };
                        
                        
                        
                        }.bind(this));
                    }
                });
                },            
          
        });
    });​
  • rabii
    Active Community Member
    • Jun 2016
    • 1250

    #2
    Hi,

    You need to trigger the action when the duration is changed however duration is a notstorable field which meaning changing on UI is not cateched (i have tried and didn't work) but what you can do is to trigger change when the endDate changes through the api not the UI interaction. see below an implementation i would consider, i have test it it works, you just need to implement your backend logic.

    PHP Code:
    define('custom:meeting-dynamic-handler', ['dynamic-handler'], function (Dep) {
    
        
        return Dep.extend({
    
            duration: null,
    
            // called on initialization
            init: function () {
                let parentType = this.model.get('parentType');
                let parentId = this.model.get('parentId');
    
                const type = this.model.getFieldType('duration');
                const dur = this.model.getFieldParam('duration', 'default');
    
                console.log('type is: ' + type);
                let collection = this.model.getClonedAttributes();
                console.log('duration default: ' + dur);
    
                this.duration = this.model.get('duration');
                // Send Ajax post request when dateEnd is changed through api
                this.recordView.listenTo(
                    this.model,
                    'change:dateEnd',
                    (model, value, options) => {
                        if (options.ui) {
                            // Skip if the change was initiated by a user interaction.
                            // Important.
                            return;
                        }
                        
                        console.log('We are here now, start the api call');
    
                        if (this.model.get('parentType') == 'Account') {
                            this.actionCalculatePricePerHour()
                        }
                    }
                );
            },
    
            // Create action a calculatePricePerHour function on a custom meeting controller which extends the existing controller.
            // Personnally i would use Action API end point, much easier and cleaner // read dpcumentation
            actionCalculatePricePerHour: function () {
                Espo.Ajax.postRequest('Account/action/calculatePricePerHour', {id: this.model.get('parentId')})
                    .then(response => {
                        // your code logic goes here
                    });
            },
        });
    });

    Hope this helps
    Rabii
    Web Dev

    Comment

    Working...