当试图重写converse.js核心函数时,我遇到了问题。下面的示例是:https://conversejs.org/docs/html/development.html#writing-a-converse-js-plugin和https://conversejs.org/docs/html/quickstart.html。我的代码如下所示:
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._super.onConnected();工作正常),但是当我想显示消息(ChatBoxView.showMessage函数)时会出错。错误消息是:TypeError: this.$是未定义的。
如何“定义”ChatBoxView这个方法?
发布于 2016-02-29 13:34:58
这个问题很可能是超级函数中的the变量绑定到错误的上下文中。
您可以通过将this._super.showMessage(attrs);更改为this._super.showMessage.apply(this, arguments);来解决这个问题。
它将使用正确的this参数和传递给重写函数的所有参数调用函数。
在sidenote上,我将更新文档以反映这一点。
https://stackoverflow.com/questions/35699318
复制相似问题