2016-02-29 85 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方法,然後這個方法將原方法作爲它的超級方法。 –