Elaborate on the right way to call super methods in plugins.

This commit is contained in:
JC Brand 2016-02-29 13:50:44 +00:00
parent c0c4cd9272
commit a4fd252a30

View File

@ -888,27 +888,38 @@ An example plugin
// For example, the inner protected *converse* object has a // For example, the inner protected *converse* object has a
// method "onConnected". You can override that method as follows: // method "onConnected". You can override that method as follows:
onConnected: function () { onConnected: function () {
// Override the onConnected method in converse.js // Overrides the onConnected method in converse.js
// You have access to the protected converse object via the
// _super attribute. // Top-level functions in "overrides" are bound to the
var converse = this._super.converse; // inner "converse" object.
var converse = this;
// Your custom code comes here. // Your custom code comes here.
// ... // ...
// You can access the original function being overridden // You can access the original function being overridden
// vai the _super attribute. // via the _super attribute.
this._super.onConnected(); // Make sure to pass on the arguments supplied to this
// function and also to apply the proper "this" object.
this._super.onConnected.apply(this, arguments);
}, },
XMPPStatus: { XMPPStatus: {
// Override converse.js's XMPPStatus Backbone model so that we can override the // Override converse.js's XMPPStatus Backbone model so that we can override the
// function that sends out the presence stanza. // function that sends out the presence stanza.
sendPresence: function (type, status_message, jid) { sendPresence: function (type, status_message, jid) {
// The "converse" object is available via the _super
// attribute.
var converse = this._super.converse; var converse = this._super.converse;
// Custom code can come here // Custom code can come here
// ... // ...
this._super.sendPresence(type, status_message, jid);
// You can call the original overridden method, by
// accessing it via the _super attribute.
// When calling it, you need to apply the proper
// context as reference by the "this" variable.
this._super.sendPresence.apply(this, arguments);
} }
}, },
} }