0

I have problem when trying to override converse.js core functions. Following this example: https://conversejs.org/docs/html/development.html#writing-a-converse-js-plugin and https://conversejs.org/docs/html/quickstart.html. My code look like this:

require(['converse'], function (converse) {
    "use strict";

    converse.plugins.add('pluginName', {
        overrides: {
            onConnected: function () {
                this._super.onConnected();
            },
            ChatBoxView: {                
                showMessage: function (attrs) {                    
                    this._super.showMessage(attrs);
                }
            }
        }
    });

    converse.initialize({
        bosh_service_url: 'http://myurl.com/http-bind/',
        i18n: locales.en,
        show_controlbox_by_default: true,
        roster_groups: true,
        keepalive: true,
        jid: 'admin@myurl.com',
        message_carbons: true,
        play_sounds: true,
        anonymous: false,
        allow_logout: false,
        authentication: 'prebind',
        prebind_url: '/prebind',
        auto_list_rooms: true
    });
});

This code partially works. Chat is displayed, it is connected (this._super.onConnected(); works fine), but I get error when I want to display message (ChatBoxView.showMessage function). Error message is: TypeError: this.$content is undefined.

How to "define" ChatBoxView this method?

1 Answers1

0

The problem is most likely that the the variable in the super function is bound to the wrong context.

You can solve that by changing this._super.showMessage(attrs); to this._super.showMessage.apply(this, arguments);.

That will call the function with the correct this parameter and all the arguments that was passed to the overriding function.

On a sidenote, I'll update the documentation to reflect this.

JC Brand
  • 2,652
  • 18
  • 18
  • Just sayin', any `this._super` approach will fail for longer inheritance chains – Bergi Mar 02 '16 at 11:46
  • No, it actually doesn't fail. In the latest code in master there is a wrapper method which sets the proper `_super` method just-in-time, before the override method is called. So if you have two overrides of the same method, they will both be called, with the most recent override method called first, and its super method will be the other override, and that one will then have the original method as its super method. – JC Brand Mar 04 '16 at 10:40