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

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

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 配置的可能方式:

<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 &quot;@mail/utils/messaging_component&quot;;
import { useUpdate } from &quot;@mail/component_hooks/use_update/use_update&quot;;
import { useComponentToModel } from &quot;@mail/component_hooks/use_component_to_model/use_component_to_model&quot;;
import { useRefToModel } from &quot;@mail/component_hooks/use_ref_to_model/use_ref_to_model&quot;;
var rpc = require(&quot;web.rpc&quot;);
const { Component } = owl;
const { useRef } = owl.hooks;

export class Chatter extends Component {
  /**
   * @override
   */
  constructor(...args) {
    super(...args);
    useUpdate({ func: () =&gt; this._update() });
    useComponentToModel({
      fieldName: &quot;component&quot;,
      modelName: &quot;mail.chatter&quot;,
      propNameAsRecordLocalId: &quot;chatterLocalId&quot;,
    });
    useRefToModel({
      fieldName: &quot;threadRef&quot;,
      modelName: &quot;mail.chatter&quot;,
      propNameAsRecordLocalId: &quot;chatterLocalId&quot;,
      refName: &quot;thread&quot;,
    });
    /**
     * 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(&quot;scrollPanel&quot;);
    this.getScrollableElement = this.getScrollableElement.bind(this);
  }
  async willStart() {
    await super.willStart();
    this.users = await this.fetchUsers();
    console.log(this.users);
  }

  async fetchUsers() {
    const model = &quot;res.users.chatter&quot;;
    const domain = [
      &quot;&amp;&quot;,
      [&quot;user&quot;, &quot;=&quot;, this.chatter.env.session.uid],
      [&quot;model_name&quot;, &quot;=&quot;, this.chatter.thread.model],
    ];
    const fields = [];
    const data = await this.env.services.rpc({
      model: model,
      method: &quot;search_read&quot;,
      args: [domain, fields],
    });
    return data;
  }

  //--------------------------------------------------------------------------
  // Public
  //--------------------------------------------------------------------------

  /**
   * @returns {mail.chatter}
   */
  get chatter() {
    return (
      this.messaging &amp;&amp;
      this.messaging.models[&quot;mail.chatter&quot;].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(&quot;o-chatter-rendered&quot;, {
      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: &quot;mail.Chatter&quot;,
});

registerMessagingComponent(Chatter);

original file:

/** @odoo-module **/

import { registerMessagingComponent } from &#39;@mail/utils/messaging_component&#39;;
import { useUpdate } from &#39;@mail/component_hooks/use_update/use_update&#39;;
import { useComponentToModel } from &#39;@mail/component_hooks/use_component_to_model/use_component_to_model&#39;;
import { useRefToModel } from &#39;@mail/component_hooks/use_ref_to_model/use_ref_to_model&#39;;

const { Component } = owl;
const { useRef } = owl.hooks;

export class Chatter extends Component {

    /**
     * @override
     */
    constructor(...args) {
        super(...args);
        useUpdate({ func: () =&gt; this._update() });
        useComponentToModel({ fieldName: &#39;component&#39;, modelName: &#39;mail.chatter&#39;, propNameAsRecordLocalId: &#39;chatterLocalId&#39; });
        useRefToModel({ fieldName: &#39;threadRef&#39;, modelName: &#39;mail.chatter&#39;, propNameAsRecordLocalId: &#39;chatterLocalId&#39;, refName: &#39;thread&#39; });
        /**
         * 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(&#39;scrollPanel&#39;);
        this.getScrollableElement = this.getScrollableElement.bind(this);
    }

    //--------------------------------------------------------------------------
    // Public
    //--------------------------------------------------------------------------

    /**
     * @returns {mail.chatter}
     */
    get chatter() {
        return this.messaging &amp;&amp; this.messaging.models[&#39;mail.chatter&#39;].get(this.props.chatterLocalId);
    }

    /**
     * @returns {Element|undefined} Scrollable Element
     */
    getScrollableElement() {
        if (!this._scrollPanelRef.el) {
            return;
        }
        return this._scrollPanelRef.el;
    }

    //--------------------------------------------------------------------------
    // Private
    //--------------------------------------------------------------------------

    /**
     * @private
     */
    _notifyRendered() {
        this.trigger(&#39;o-chatter-rendered&#39;, {
            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: &#39;mail.Chatter&#39;,
});

registerMessagingComponent(Chatter);

I try to overwrite adding to manifest.py:

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

but have an error :

UncaughtPromiseError
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 组件之前取消注册旧的聊天组件。

示例:

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 &quot;@mail/components/chatter/chatter&quot;;
import { registerMessagingComponent, unregisterMessagingComponent } from &quot;@mail/utils/messaging_component&quot;;

// ...

unregisterMessagingComponent(OldChatter);
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:

确定