PDA

View Full Version : More maintainable way to call base methods when inheriting



raygun
31 Mar 2011, 8:48 PM
This seems like a more maintainable way to call base methods when inheriting:


Ext.ux.MyPanel = Ext.extend(Ext.Panel,
{
initComponent: function () {
// call base method. All that changes is the method name been called, everything else stays.
this.constructor.superclass.initComponent.apply(this, arguments);

...
}
});

I'd like to know if there would be any issues. Seems to work fine.

mankz
1 Apr 2011, 12:49 AM
First, try subclassing Ext.ux.MyPanel and see what happens :)

Second, read this: http://www.sencha.com/forum/showthread.php?117497-Accessing-superclass-methods&

raygun
3 Apr 2011, 9:51 PM
Humpf, you are very correct.
Thanks for the heads up.

I've tried a couple things. This seems satisfactory.
Should maybe improve performance a little.


Ext.namespace('Ext.ux');
(function () {
var base = Ext.Panel.prototype;

Ext.ux.MyClass = Ext.extend(Ext.Panel, {
initComponent: function () {
base.initComponent.apply(this, arguments);

...
}
}
})();

raygun
3 Apr 2011, 11:57 PM
Or as little repetition as possible:
(but less readable)


Ext.namespace('Ext.ux');
(function (baseClass) {
var base = baseClass.prototype;

Ext.ux.MyClass = Ext.extend(baseClass, {
initComponent: function () {
base.initComponent.apply(this, arguments);

...
}
}
})(Ext.Panel);