javascript - 如何覆盖 Odoo 15 中添加到 core.action_registry 的 JS 函数?

标签 javascript odoo odoo-15

hr_attendance 模块将抽象操作 hr_attendance_my_attendances 添加到 core.action_registry 中,该操作具有名为 update_attendance 的函数。我想重写这个函数。我尝试了很多事情,但没有结果。这是 @Kenly ( How to override js method in Odoo 15? ) 的一个很好的例子,但不幸的是我无法使用它,因为 core.action_registry 没有函数 extend

这就是我现在所拥有的(在我的模块中,在名为 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;

});

但它似乎什么也没做,因为我在 JS 控制台中看不到 I HAVE OVERWRITTEN THE FUNCTION 消息。

而且我认为我已经将 JS 代码添加到了我的模块中,因为如果我写了错误的代码,我就会收到错误。但为了以防万一,我已将 JS 文件添加到 __manifest__.py 中,其中包含以下几行:

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

谁能给我一点线索吗?

最佳答案

您创建了一个未使用的新操作。

覆盖 update_attendance函数,使用 include

示例:

/** @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' });
            }
        });
    },
});

关于javascript - 如何覆盖 Odoo 15 中添加到 core.action_registry 的 JS 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77307887/

相关文章:

javascript - 如何使用 jQuery 在网页中交换两个图像(连续两次点击)?

python - ValueError : Expected singleton: res. 合作伙伴(1, 12, 29, 30, 36) - res.partner - Odoo v10 社区

python - 模型 "res.config.settings"中不存在字段

database - 在odoo中按登录用户选择数据库

javascript - 打开一个盒子而不是新窗口

javascript - Ajax:表单数据未传递到电子邮件

javascript - 如何在 TypeScript 中动态添加项目到元组?

xml - POS 模板屏幕中的 t-foreach 循环不起作用

python - 我怎样才能在odoo中编辑 "catch" Action