Debasish
Debasish

Reputation: 322

How to Override mail.chatter js of mail module in odoo 9?

I want to override the ChatterComposer function i.e.

init: function (parent, dataset, options) {
    this._super(parent, options);
    this.thread_dataset = dataset;
    this.suggested_partners = [];
    this.options = _.defaults(this.options, {
        display_mode: 'textarea',
        record_name: false,
        is_log: false,
        internal_subtypes: [],
    });
    if (this.options.is_log) {
        this.options.send_text = _t('Log');
    }
    this.events = _.extend(this.events, {
        'click .o_composer_button_full_composer': 'on_open_full_composer',
    });
},

and Chatter function i.e

 open_composer: function (options) {
    var self = this;
    var old_composer = this.composer;
    // create the new composer
    this.composer = new ChatterComposer(this, this.thread_dataset, {
        context: this.context,
        input_min_height: 50,
        input_max_height: Number.MAX_VALUE, // no max_height limit for the chatter
        input_baseline: 14,
        internal_subtypes: this.options.internal_subtypes,
        is_log: options && options.is_log,
        record_name: this.record_name,
        default_body: old_composer && old_composer.$input && old_composer.$input.val(),
        default_mention_selections: old_composer && old_composer.mention_get_listener_selections(),
    });
    this.composer.on('input_focused', this, function () {
        this.composer.mention_set_prefetched_partners(this.mention_suggestions || []);
    });
    this.composer.insertBefore(this.$('.o_mail_thread')).then(function () {
        // destroy existing composer
        if (old_composer) {
            old_composer.destroy();
        }
        if (!config.device.touch) {
            self.composer.focus();
        }
        self.composer.on('post_message', self, self.on_post_message);
        self.composer.on('need_refresh', self, self.refresh_followers);
        self.composer.on('close_composer', null, self.close_composer.bind(self, true));
    });
    this.mute_new_message_button(true);
},

of chatter.js .

So that i tried the below code, but it show an error, i.e. Some modules could not be started and in error section shows, Cannot read property 'extend' of undefined

odoo.define('demo.call', function (require) {
"use strict";
var composer = require('mail.composer');
var chatter = require('mail.Chatter');
chatter.ChatterComposer.extend({
 //code here
 });
});

When i use extend, it throws an error, Cannot read property 'extend' of undefined, and when i use include instead of extent, it throws an error Cannot read property 'include' of undefined. Please share with me any idea, how to resolve this.

Upvotes: 0

Views: 325

Answers (1)

Lucas
Lucas

Reputation: 894

If you look at chatter.js, you will see that it only returns Chatter. And on the Chatter code you will see that the instance of the ChatterComposer that it holds is called composer:

this.composer = new ChatterComposer(...)

And that's why chatter.ChatterComposer is undefined on your code.

If you must override ChatterComposer's init method, then you can modify the chatter.js return, to return both, the Chatter and the ChatterComposer like so:

return {
    'Chatter': Chatter,
    'ChatterComposer': ChatterComposer,
}

However, you will need to fix all references to require('mail.Chatter'), changing it to require('mail.Chatter').Chatter...

Instead, I would recommend you creating your own composer, like so (you can even copy the current ChatterComposer and modify just the init method):

var MyCustomChatterComposer = form_common.AbstractField.extend({...})

And then replacing on Chatter by overriding the open_composer function, and calling your custom chatter composer:

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

chatter.open_composer = function (options) {
    ...
    chatter.composer = new MyCustomChatterComposer();
    ...
}

Upvotes: 2

Related Questions