英文:
Odoo 15 - overwrite .js file in Owl framework (Chatter Component)
问题
-
可以覆盖整个 .js 文件,方法是将您的定制 js 文件放入正确的位置并确保它被正确加载。在您的例子中,您试图覆盖 chatter.js 文件,但遇到了一个已经注册的组件名称冲突问题。为了成功覆盖,您可以尝试以下步骤:
- 确保您的自定义 chatter.js 文件位于正确的位置,即 "freshdesk-connector/static/src/components/chatter/chatter.js"。
- 确保您的模块中没有与 "Chatter" 相关的其他组件注册。根据错误消息,似乎已经有一个名为 "Chatter" 的组件注册。您需要检查您的模块代码,确保没有重复注册相同名称的组件。
- 在您的自定义 chatter.js 文件中,确保您的组件被正确命名,不要与现有的 chatter.js 中的组件名称冲突。您可以尝试将组件名称更改为不同的名称,以避免冲突。
-
使用 patch 方法来覆盖 Chatter 组件通常是一个好方法,但您可能需要正确设置 patch 以确保覆盖发生。以下是一个示例 patch 配置的可能方式:
<odoo>
<data>
<template id="assets_backend_custom" name="Custom Backend Assets" inherit_id="web.assets_backend">
<xpath expr="." position="inside">
<script type="text/javascript" src="/freshdesk-connector/static/src/components/chatter/chatter.js"></script>
</xpath>
</template>
</data>
</odoo>
在这个示例中,我们使用 xpath
来将您的自定义 chatter.js 文件添加到现有的 web.assets_backend
资产中。确保 src
属性指向您的自定义 chatter.js 文件的正确路径。
请确保您的模块的 XML 配置正确加载,且没有其他地方覆盖了 Chatter 组件。如果其他模块或配置也尝试修改 Chatter 组件,可能会导致冲突。
注意:以上只是一种可能的方法,实际实现可能会因您的 Odoo 版本和模块结构而有所不同。确保您的定制方式与您的 Odoo 版本和项目配置一致。
英文:
I want to overwrite chatter.js (module: mail/static/src/components/chatter/chatter.js) in odoo 15.
I want to add there user_chatter_status so, this how look my modified file :
/** @odoo-module **/
import { registerMessagingComponent } from "@mail/utils/messaging_component";
import { useUpdate } from "@mail/component_hooks/use_update/use_update";
import { useComponentToModel } from "@mail/component_hooks/use_component_to_model/use_component_to_model";
import { useRefToModel } from "@mail/component_hooks/use_ref_to_model/use_ref_to_model";
var rpc = require("web.rpc");
const { Component } = owl;
const { useRef } = owl.hooks;
export class Chatter extends Component {
/**
* @override
*/
constructor(...args) {
super(...args);
useUpdate({ func: () => this._update() });
useComponentToModel({
fieldName: "component",
modelName: "mail.chatter",
propNameAsRecordLocalId: "chatterLocalId",
});
useRefToModel({
fieldName: "threadRef",
modelName: "mail.chatter",
propNameAsRecordLocalId: "chatterLocalId",
refName: "thread",
});
/**
* Reference of the scroll Panel (Real scroll element). Useful to pass the Scroll element to
* child component to handle proper scrollable element.
*/
this._scrollPanelRef = useRef("scrollPanel");
this.getScrollableElement = this.getScrollableElement.bind(this);
}
async willStart() {
await super.willStart();
this.users = await this.fetchUsers();
console.log(this.users);
}
async fetchUsers() {
const model = "res.users.chatter";
const domain = [
"&",
["user", "=", this.chatter.env.session.uid],
["model_name", "=", this.chatter.thread.model],
];
const fields = [];
const data = await this.env.services.rpc({
model: model,
method: "search_read",
args: [domain, fields],
});
return data;
}
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
/**
* @returns {mail.chatter}
*/
get chatter() {
return (
this.messaging &&
this.messaging.models["mail.chatter"].get(this.props.chatterLocalId)
);
}
get user_chatter_status() {
return this.users;
}
/**
* @returns {Element|undefined} Scrollable Element
*/
getScrollableElement() {
if (!this._scrollPanelRef.el) {
return;
}
return this._scrollPanelRef.el;
}
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
/**
* @private
*/
_notifyRendered() {
this.trigger("o-chatter-rendered", {
attachments: this.chatter.thread.allAttachments,
thread: this.chatter.thread.localId,
});
}
/**
* @private
*/
_update() {
if (!this.chatter) {
return;
}
if (this.chatter.thread) {
this._notifyRendered();
}
}
}
Object.assign(Chatter, {
props: {
chatterLocalId: String,
},
template: "mail.Chatter",
});
registerMessagingComponent(Chatter);
original file:
/** @odoo-module **/
import { registerMessagingComponent } from '@mail/utils/messaging_component';
import { useUpdate } from '@mail/component_hooks/use_update/use_update';
import { useComponentToModel } from '@mail/component_hooks/use_component_to_model/use_component_to_model';
import { useRefToModel } from '@mail/component_hooks/use_ref_to_model/use_ref_to_model';
const { Component } = owl;
const { useRef } = owl.hooks;
export class Chatter extends Component {
/**
* @override
*/
constructor(...args) {
super(...args);
useUpdate({ func: () => this._update() });
useComponentToModel({ fieldName: 'component', modelName: 'mail.chatter', propNameAsRecordLocalId: 'chatterLocalId' });
useRefToModel({ fieldName: 'threadRef', modelName: 'mail.chatter', propNameAsRecordLocalId: 'chatterLocalId', refName: 'thread' });
/**
* Reference of the scroll Panel (Real scroll element). Useful to pass the Scroll element to
* child component to handle proper scrollable element.
*/
this._scrollPanelRef = useRef('scrollPanel');
this.getScrollableElement = this.getScrollableElement.bind(this);
}
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
/**
* @returns {mail.chatter}
*/
get chatter() {
return this.messaging && this.messaging.models['mail.chatter'].get(this.props.chatterLocalId);
}
/**
* @returns {Element|undefined} Scrollable Element
*/
getScrollableElement() {
if (!this._scrollPanelRef.el) {
return;
}
return this._scrollPanelRef.el;
}
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
/**
* @private
*/
_notifyRendered() {
this.trigger('o-chatter-rendered', {
attachments: this.chatter.thread.allAttachments,
thread: this.chatter.thread.localId,
});
}
/**
* @private
*/
_update() {
if (!this.chatter) {
return;
}
if (this.chatter.thread) {
this._notifyRendered();
}
}
}
Object.assign(Chatter, {
props: {
chatterLocalId: String,
},
template: 'mail.Chatter',
});
registerMessagingComponent(Chatter);
I try to overwrite adding to manifest.py:
"assets": {
"web.assets_backend": [
"freshdesk-connector/static/src/components/chatter/chatter.js",
],
but have an error :
UncaughtPromiseError
Uncaught Promise > There already is a registered component with the name "Chatter"
I try also to use patch but this not work for me (i think i use this bad, because I'm 'dummy' from .js).
So the main questions is:
- Is it possible to overwrite whole .js file, if yes how to do this.
- How to use patch method to overwrite Chatter component ?
答案1
得分: 0
你可以在注册新的 Chatter
组件之前取消注册旧的聊天组件。
示例:
import { Chatter as OldChatter } from "@mail/components/chatter/chatter";
import { registerMessagingComponent, unregisterMessagingComponent } from "@mail/utils/messaging_component";
// ...
unregisterMessagingComponent(OldChatter);
registerMessagingComponent(Chatter);
英文:
You can unregister the old chat component before registering the new Chatter
Example:
import { Chatter as OldChatter } from "@mail/components/chatter/chatter";
import { registerMessagingComponent, unregisterMessagingComponent } from "@mail/utils/messaging_component";
// ...
unregisterMessagingComponent(OldChatter);
registerMessagingComponent(Chatter);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论