1

How could I inherit/override a function in Odoo 16 javascript example

In the addons/web/static/src/views/fields/formatters.js there is a function

export function formatFloat(value, options = {}) {
if (value === false) {
return "";
}
if (options.humanReadable) {
  return humanNumber(value, options);
}
const grouping = options.grouping || l10n.grouping;
const thousandsSep = "thousandsSep" in options ? options.thousandsSep : l10n.thousandsSep;
const decimalPoint = "decimalPoint" in options ? options.decimalPoint : l10n.decimalPoint;
let precision;
if (options.digits && options.digits[1] !== undefined) {
precision = options.digits[1];
} else {
   precision = 2;
}
const formatted = (value || 0).toFixed(precision).split(".");
formatted[0] = insertThousandsSep(formatted[0], thousandsSep, grouping);
if (options.noTrailingZeros) {
formatted[1] = formatted[1].replace(/0+$/, "");
}
return formatted[1] ? formatted.join(decimalPoint) : formatted[0];
}

How could I inherit/override this function?

Tried to add the same function name in my module and export it again, but it didn't work.

1 Answers1

0
import NeededPatchModule from '@module_name/../../main';
//example - import { AttendeeCalendarController } from "@calendar/views/attendee_calendar/attendee_calendar_controller";

import { patch } from '@web/core/utils/patch';

patch(NeededPatchModule.prototype, 'Your comment', {
    /**
     * @override
     */
    Func(args) {
        // Your code logic
        return this._super(...arguments);
    }
})
Dkodsy
  • 1
  • Dear Dkodsy, Thank you for your answer, I've tried it but unfortunately, it doesn't work. import {formatFloat} from '@web/views/fields/formatters'; import { patch } from '@web/core/utils/patch'; patch(formatFloat.prototype, 'Test', { /** * @override */ Func(value, options = {}) { console.log(111111111111); return this._super(value, options); } }) – Ahmad Zarour Mar 12 '23 at 08:29
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 16 '23 at 12:05