How can I overwrite a JS function added to core.action_registry in Odoo 15?

296 views Asked by At

The hr_attendance module adds to core.action_registry the abstract action hr_attendance_my_attendances, which has a function named update_attendance. I want to overwrite this function. I've tried a lot of things, with no result. Here's a good example by @Kenly (How to override js method in Odoo 15?), but unfortunately I can't use it because core.action_registry has no function extend.

This is what I have now (in my module, in a file named my_attendances.js):

odoo.define('my_module.my_attendances', function (require) {
    "use strict";
    
    const session = require('web.session');
    var core = require('web.core');
    var MyAttendances = require('hr_attendance.my_attendances');

    var MyAttendancesExtended = MyAttendances.extend({

        // events: _.extend({
        //     'click .o_hr_attendance_sign_in_out_icon': 'update_attendance',
        // }, AbstractField.prototype.events),

        update_attendance: function () {
            console.log('I HAVE OVERWRITTEN THE FUNCTION');
            var self = this;
            this._rpc({
                model: 'hr.employee',
                method: 'attendance_manual',
                args: [[self.employee.id], 'hr_attendance.hr_attendance_action_my_attendances', None],
                context: session.user_context,
            })
            .then(function(result) {
                if (result.action) {
                    self.do_action(result.action);
                } else if (result.warning) {
                    self.displayNotification({ title: result.warning, type: 'danger' });
                }
            });
        },
    });
    
    core.action_registry.add('my_module.my_attendances', MyAttendancesExtended);
    
    return MyAttendancesExtended;

});

But it seems to do nothing, since I cannot see the I HAVE OVERWRITTEN THE FUNCTION message in the JS console.

And I think I've added fine the JS code to my module, since I get errors if I write wrong code. But just in case, I've added the JS file to the __manifest__.py with these lines:

'assets': {
    'web.assets_backend': [
        'my_module/static/src/js/my_attendance.js',
    ],
},

Anyone can give me a clue please?

1

There are 1 answers

1
Kenly On BEST ANSWER

You created a new action that is not used.

To override update_attendance function, use include

Example:

/** @odoo-module **/

import MyAttendances from 'hr_attendance.my_attendances';
import session from 'web.session';

MyAttendances.include({

    update_attendance: function () {
        console.log('I HAVE OVERWRITTEN THE FUNCTION');
        var self = this;
        this._rpc({
            model: 'hr.employee',
            method: 'attendance_manual',
            args: [[self.employee.id], 'hr_attendance.hr_attendance_action_my_attendances', null],
            context: session.user_context,
        })
        .then(function(result) {
            if (result.action) {
                self.do_action(result.action);
            } else if (result.warning) {
                self.displayNotification({ title: result.warning, type: 'danger' });
            }
        });
    },
});