Odoo 15 – 在Owl框架中覆盖.js文件(Chatter组件)

huangapple go评论99阅读模式
英文:

Odoo 15 - overwrite .js file in Owl framework (Chatter Component)

问题

  1. 可以覆盖整个 .js 文件,方法是将您的定制 js 文件放入正确的位置并确保它被正确加载。在您的例子中,您试图覆盖 chatter.js 文件,但遇到了一个已经注册的组件名称冲突问题。为了成功覆盖,您可以尝试以下步骤:

    • 确保您的自定义 chatter.js 文件位于正确的位置,即 "freshdesk-connector/static/src/components/chatter/chatter.js"。
    • 确保您的模块中没有与 "Chatter" 相关的其他组件注册。根据错误消息,似乎已经有一个名为 "Chatter" 的组件注册。您需要检查您的模块代码,确保没有重复注册相同名称的组件。
    • 在您的自定义 chatter.js 文件中,确保您的组件被正确命名,不要与现有的 chatter.js 中的组件名称冲突。您可以尝试将组件名称更改为不同的名称,以避免冲突。
  2. 使用 patch 方法来覆盖 Chatter 组件通常是一个好方法,但您可能需要正确设置 patch 以确保覆盖发生。以下是一个示例 patch 配置的可能方式:

  1. <odoo>
  2. <data>
  3. <template id="assets_backend_custom" name="Custom Backend Assets" inherit_id="web.assets_backend">
  4. <xpath expr="." position="inside">
  5. <script type="text/javascript" src="/freshdesk-connector/static/src/components/chatter/chatter.js"></script>
  6. </xpath>
  7. </template>
  8. </data>
  9. </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 :

  1. /** @odoo-module **/
  2. import { registerMessagingComponent } from &quot;@mail/utils/messaging_component&quot;;
  3. import { useUpdate } from &quot;@mail/component_hooks/use_update/use_update&quot;;
  4. import { useComponentToModel } from &quot;@mail/component_hooks/use_component_to_model/use_component_to_model&quot;;
  5. import { useRefToModel } from &quot;@mail/component_hooks/use_ref_to_model/use_ref_to_model&quot;;
  6. var rpc = require(&quot;web.rpc&quot;);
  7. const { Component } = owl;
  8. const { useRef } = owl.hooks;
  9. export class Chatter extends Component {
  10. /**
  11. * @override
  12. */
  13. constructor(...args) {
  14. super(...args);
  15. useUpdate({ func: () =&gt; this._update() });
  16. useComponentToModel({
  17. fieldName: &quot;component&quot;,
  18. modelName: &quot;mail.chatter&quot;,
  19. propNameAsRecordLocalId: &quot;chatterLocalId&quot;,
  20. });
  21. useRefToModel({
  22. fieldName: &quot;threadRef&quot;,
  23. modelName: &quot;mail.chatter&quot;,
  24. propNameAsRecordLocalId: &quot;chatterLocalId&quot;,
  25. refName: &quot;thread&quot;,
  26. });
  27. /**
  28. * Reference of the scroll Panel (Real scroll element). Useful to pass the Scroll element to
  29. * child component to handle proper scrollable element.
  30. */
  31. this._scrollPanelRef = useRef(&quot;scrollPanel&quot;);
  32. this.getScrollableElement = this.getScrollableElement.bind(this);
  33. }
  34. async willStart() {
  35. await super.willStart();
  36. this.users = await this.fetchUsers();
  37. console.log(this.users);
  38. }
  39. async fetchUsers() {
  40. const model = &quot;res.users.chatter&quot;;
  41. const domain = [
  42. &quot;&amp;&quot;,
  43. [&quot;user&quot;, &quot;=&quot;, this.chatter.env.session.uid],
  44. [&quot;model_name&quot;, &quot;=&quot;, this.chatter.thread.model],
  45. ];
  46. const fields = [];
  47. const data = await this.env.services.rpc({
  48. model: model,
  49. method: &quot;search_read&quot;,
  50. args: [domain, fields],
  51. });
  52. return data;
  53. }
  54. //--------------------------------------------------------------------------
  55. // Public
  56. //--------------------------------------------------------------------------
  57. /**
  58. * @returns {mail.chatter}
  59. */
  60. get chatter() {
  61. return (
  62. this.messaging &amp;&amp;
  63. this.messaging.models[&quot;mail.chatter&quot;].get(this.props.chatterLocalId)
  64. );
  65. }
  66. get user_chatter_status() {
  67. return this.users;
  68. }
  69. /**
  70. * @returns {Element|undefined} Scrollable Element
  71. */
  72. getScrollableElement() {
  73. if (!this._scrollPanelRef.el) {
  74. return;
  75. }
  76. return this._scrollPanelRef.el;
  77. }
  78. //--------------------------------------------------------------------------
  79. // Private
  80. //--------------------------------------------------------------------------
  81. /**
  82. * @private
  83. */
  84. _notifyRendered() {
  85. this.trigger(&quot;o-chatter-rendered&quot;, {
  86. attachments: this.chatter.thread.allAttachments,
  87. thread: this.chatter.thread.localId,
  88. });
  89. }
  90. /**
  91. * @private
  92. */
  93. _update() {
  94. if (!this.chatter) {
  95. return;
  96. }
  97. if (this.chatter.thread) {
  98. this._notifyRendered();
  99. }
  100. }
  101. }
  102. Object.assign(Chatter, {
  103. props: {
  104. chatterLocalId: String,
  105. },
  106. template: &quot;mail.Chatter&quot;,
  107. });
  108. registerMessagingComponent(Chatter);

original file:

  1. /** @odoo-module **/
  2. import { registerMessagingComponent } from &#39;@mail/utils/messaging_component&#39;;
  3. import { useUpdate } from &#39;@mail/component_hooks/use_update/use_update&#39;;
  4. import { useComponentToModel } from &#39;@mail/component_hooks/use_component_to_model/use_component_to_model&#39;;
  5. import { useRefToModel } from &#39;@mail/component_hooks/use_ref_to_model/use_ref_to_model&#39;;
  6. const { Component } = owl;
  7. const { useRef } = owl.hooks;
  8. export class Chatter extends Component {
  9. /**
  10. * @override
  11. */
  12. constructor(...args) {
  13. super(...args);
  14. useUpdate({ func: () =&gt; this._update() });
  15. useComponentToModel({ fieldName: &#39;component&#39;, modelName: &#39;mail.chatter&#39;, propNameAsRecordLocalId: &#39;chatterLocalId&#39; });
  16. useRefToModel({ fieldName: &#39;threadRef&#39;, modelName: &#39;mail.chatter&#39;, propNameAsRecordLocalId: &#39;chatterLocalId&#39;, refName: &#39;thread&#39; });
  17. /**
  18. * Reference of the scroll Panel (Real scroll element). Useful to pass the Scroll element to
  19. * child component to handle proper scrollable element.
  20. */
  21. this._scrollPanelRef = useRef(&#39;scrollPanel&#39;);
  22. this.getScrollableElement = this.getScrollableElement.bind(this);
  23. }
  24. //--------------------------------------------------------------------------
  25. // Public
  26. //--------------------------------------------------------------------------
  27. /**
  28. * @returns {mail.chatter}
  29. */
  30. get chatter() {
  31. return this.messaging &amp;&amp; this.messaging.models[&#39;mail.chatter&#39;].get(this.props.chatterLocalId);
  32. }
  33. /**
  34. * @returns {Element|undefined} Scrollable Element
  35. */
  36. getScrollableElement() {
  37. if (!this._scrollPanelRef.el) {
  38. return;
  39. }
  40. return this._scrollPanelRef.el;
  41. }
  42. //--------------------------------------------------------------------------
  43. // Private
  44. //--------------------------------------------------------------------------
  45. /**
  46. * @private
  47. */
  48. _notifyRendered() {
  49. this.trigger(&#39;o-chatter-rendered&#39;, {
  50. attachments: this.chatter.thread.allAttachments,
  51. thread: this.chatter.thread.localId,
  52. });
  53. }
  54. /**
  55. * @private
  56. */
  57. _update() {
  58. if (!this.chatter) {
  59. return;
  60. }
  61. if (this.chatter.thread) {
  62. this._notifyRendered();
  63. }
  64. }
  65. }
  66. Object.assign(Chatter, {
  67. props: {
  68. chatterLocalId: String,
  69. },
  70. template: &#39;mail.Chatter&#39;,
  71. });
  72. registerMessagingComponent(Chatter);

I try to overwrite adding to manifest.py:

  1. &quot;assets&quot;: {
  2. &quot;web.assets_backend&quot;: [
  3. &quot;freshdesk-connector/static/src/components/chatter/chatter.js&quot;,
  4. ],

but have an error :

  1. UncaughtPromiseError
  2. Uncaught Promise &gt; There already is a registered component with the name &quot;Chatter&quot;

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:

  1. Is it possible to overwrite whole .js file, if yes how to do this.
  2. How to use patch method to overwrite Chatter component ?

答案1

得分: 0

你可以在注册新的 Chatter 组件之前取消注册旧的聊天组件。

示例:

  1. import { Chatter as OldChatter } from "@mail/components/chatter/chatter";
  2. import { registerMessagingComponent, unregisterMessagingComponent } from "@mail/utils/messaging_component";
  3. // ...
  4. unregisterMessagingComponent(OldChatter);
  5. registerMessagingComponent(Chatter);
英文:

You can unregister the old chat component before registering the new Chatter

Example:

  1. import { Chatter as OldChatter } from &quot;@mail/components/chatter/chatter&quot;;
  2. import { registerMessagingComponent, unregisterMessagingComponent } from &quot;@mail/utils/messaging_component&quot;;
  3. // ...
  4. unregisterMessagingComponent(OldChatter);
  5. registerMessagingComponent(Chatter);

huangapple
  • 本文由 发表于 2023年7月10日 16:21:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76651948.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定