Search code examples
odooodoo-15

How to override mail.model js class function in odoo?


in file "addons/mail/static/src/models/message_action_list/message_action_list.js" there is a js class:

/** @odoo-module **/

import { registerNewModel } from '@mail/model/model_core';
import { attr, many2one, one2one } from '@mail/model/model_field';
import { clear, insertAndReplace, replace } from '@mail/model/model_field_command';
import { markEventHandled } from '@mail/utils/utils';

function factory(dependencies) {

    class MessageActionList extends dependencies['mail.model'] {

        /**
         * @private
         * @returns {boolean}
         */
        _computeHasReplyIcon() {
            ...
        }

        ...

    }

    MessageActionList.fields = {
        ...
    };
    MessageActionList.identifyingFields = ['messageView'];
    MessageActionList.modelName = 'mail.message_action_list';

    return MessageActionList;
}

registerNewModel('mail.message_action_list', factory);

I want to override a _computeHasReplyIcon() function.

how can I do this?


Solution

  • You can use the registerInstancePatchModel to override the _computeHasReplyIcon function as they did in the im_livechat module:

    /** @odoo-module **/
    
    import { registerInstancePatchModel } from '@mail/model/model_core';
    
    registerInstancePatchModel('mail.message_action_list', 'im_livechat/static/src/models/message_action_list/message_action_list.js', {
    
        //----------------------------------------------------------------------
        // Private
        //----------------------------------------------------------------------
    
        /**
         * @override
         */
        _computeHasReplyIcon() {
            if (
                this.message &&
                this.message.originThread &&
                this.message.originThread.channel_type === 'livechat'
            ) {
                return false;
            }
            return this._super();
        }
    });