Odoo change base javascript method

2019-08-23 03:47发布

There is a file in Odoo: addons/mail/static/src/js/chatter.js . It contains a method I would like to change: message_get_suggested_recipients.

For this I created an addon with files:

chatter.xml

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data>
        <!--template id="assets_backend" name="addon1" inherit_id="web.assets_backend"-->
            <xpath expr="." position="inside">
                <script type="text/javascript" src="/addon1/static/src/js/chatter.js"></script>
            </xpath>
        </template>
    </data>
</odoo>

and

chatter.js

odoo.define('addon1.chatter', function(require){
'use strict';

var OdooChatter = require('mail.Chatter');

OdooChatter.include({
    message_get_suggested_recipients: function () {
    var self = this;
    var email_addresses = _.pluck(this.suggested_partners, 'email_address');
    return this.thread_dataset
        .call('message_get_suggested_recipients', [[this.context.default_res_id], this.context])
        .done(function (suggested_recipients) {
            var thread_recipients = suggested_recipients[self.context.default_res_id];
            _.each(thread_recipients, function (recipient) {
                var parsed_email = utils.parse_email(recipient[1]);
                if (_.indexOf(email_addresses, parsed_email[1]) === -1) {
                    self.suggested_partners.push({
                        checked: recipient[3] || true,
                        partner_id: recipient[0],
                        full_name: recipient[1],
                        name: parsed_email[0],
                        email_address: parsed_email[1],
                        reason: recipient[2],
                    });
                }
            });
        });
    }
});
});

I can see that javascript loads when Odoo website is open, but breakpoints don't catch the right place, that means loaded javascript was ineffective.

How to change the method because my provided method isn't called (instead the original is called)?

1条回答
\"骚年 ilove
2楼-- · 2019-08-23 04:27

Modify your chatter.xml file as:

<template id="your_module.assets_backend" inherit_id="web.assets_backend" name="Your custome name">
    <xpath expr="//script[@src='/mail/static/src/js/chatter.js']" position="after">
        <script type="text/javascript" src="/addon1/static/src/js/chatter.js"></script>
    </xpath>
</template>

This works in my case on Odoo 11. In your case (Odoo 10) it should work as well.


After re-looking at the Odoo's source code, because you'd like to override the message_get_suggested_recipients which is a prop of mail.composer.BasicComposer (and not mail.Chatter) I think your include should be:

var composer = require('mail.composer');

var ChatterComposer = composer.BasicComposer.include(
    // This goes your work of message_get_suggested_recipients rewrite.
);
查看更多
登录 后发表回答