2016-02-29 108 views
0

我在尝试覆盖converse.js核心功能时遇到问题。以下示例:https://conversejs.org/docs/html/development.html#writing-a-converse-js-pluginhttps://conversejs.org/docs/html/quickstart.html。我的代码如下所示:无法覆盖converse.js核心功能

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: '[email protected]', 
     message_carbons: true, 
     play_sounds: true, 
     anonymous: false, 
     allow_logout: false, 
     authentication: 'prebind', 
     prebind_url: '/prebind', 
     auto_list_rooms: true 
    }); 
}); 

此代码部分工作。聊天显示,它连接(this._super.onConnected();工作正常),但当我想显示消息(ChatBoxView.showMessage函数)时出现错误。错误讯息是:TypeError:This。$ content is undefined

如何“定义”ChatBoxView这种方法?

回答

1

问题很可能是超级函数中的the变量绑定了错误的上下文。

您可以通过将this._super.showMessage(attrs);更改为this._super.showMessage.apply(this, arguments);来解决该问题。

这将调用带有正确this参数的函数以及传递给覆盖函数的所有参数。

在旁注上,我将更新文档以反映此情况。

+0

谢谢,JC品牌。它的工作就像一个魅力:) – Dusan

+0

只是说,'任何'this._super'方法将失败更长的继承链 – Bergi

+0

不,它实际上不会失败。在master中的最新代码中,有一个包装器方法,它在调用override方法之前及时地设置适当的_super方法。所以,如果你有两个覆盖同一个方法的方法,它们都会被调用,最近的override方法被称为first方法,它的super方法将是另一个override方法,然后这个方法将原方法作为它的超级方法。 –