Pular para o conteúdo
Menu
Esta pergunta foi sinalizada
2 Respostas
2623 Visualizações

Good evening everyone, I have very little knowledge of JavaScript and I am new to Odoo 17.

I want to modify the following value:

const canCreate = options.no_create ? false : hasCreatePermission;

which is found here

export const many2OneField = {
component: Many2OneField,
displayName: _t("Many2one"),
supportedOptions: [
{
label: _t("Disable opening"),
name: "no_open",
type: "boolean",
},
{
label: _t("Disable creation"),
name: "no_create",
type: "boolean",
help: _t(
"If checked, users won't be able to create records through the autocomplete dropdown at all."
),
},
{
label: _t("Disable 'Create' option"),
name: "no_quick_create",
type: "boolean",
help: _t(
"If checked, users will not be able to create records based on the text input; they will still be able to create records via a popup form."
),
},
{
label: _t("Disable 'Create and Edit' option"),
name: "no_create_edit",
type: "boolean",
help: _t(
"If checked, users will not be able to create records based through a popup form; they will still be able to create records based on the text input."
),
},
],
supportedTypes: ["many2one"],
extractProps({ attrs, context, decorations, options, string }, dynamicInfo) {
const hasCreatePermission = attrs.can_create ? evaluateBooleanExpr(attrs.can_create) : true;
const hasWritePermission = attrs.can_write ? evaluateBooleanExpr(attrs.can_write) : true;
const canCreate = options.no_create ? false : hasCreatePermission;
return {
placeholder: attrs.placeholder,
canOpen: !options.no_open,
canCreate,
canWrite: hasWritePermission,
canQuickCreate: canCreate && !options.no_quick_create,
canCreateEdit: canCreate && !options.no_create_edit,
context: context,
decorations,
domain: dynamicInfo.domain,
nameCreateField: options.create_name_field,
canScanBarcode: !!options.can_scan_barcode,
string,
};
},
};

The code is located in odoo\addons\web\static\src\views\fields\many2one\many2one_field.js.

While researching, I came across discussions that recommend using patching to extend the object.

My patching code:

/** @odoo-module **/

import {many2OneField} from "@web/views/fields/many2one/many2one_field";
import { patch } from "@web/core/utils/patch";


patch(many2OneField, {
extractProps() {
let pros = super.extractProps()
pros.options.canCreate = true
return pros;
},
});

I don't know why it doesn't work, any help is welcome.

Thank you.



Avatar
Cancelar
Melhor resposta

Hi,

Please refer to the following blog for an understanding of the workings of patching, with an example:

https://www.cybrosys.com/blog/how-to-patch-existing-owl-component-in-odoo-17


Hope this helps.

Avatar
Cancelar
Melhor resposta

Hello, 
In newer odoo, native javascript module, you can extend the option using extends keywords as you extends javascript class. 


For example here:

/** @odoo-module **/  // -> This indicate it's an odoo module. REQUIRED FOR ODOO MODULE.
import { registry } from '@web/core/registry'; //-> Use registry to register new fields
import {Many2OneField} from "@web/views/fields/many2one/many2one_field";; //-> Fields that's we inherite

export class YourNewField extends Many2OneField { //-> In here, we inherit the fields
​ extractProps({ attrs, context, decorations, options, string }, dynamicInfo) {
res = ​super.extractProps()
​​res['canCreate'] = true
​} }
// After that don't forget to register your new widget
registry.category("fields").add("YourNewField", YourNewField);
Avatar
Cancelar
Publicações relacionadas Respostas Visualizações Atividade
0
nov. 24
1126
2
jan. 25
926
0
nov. 24
1186
1
nov. 24
4680
1
mai. 25
1501