PDA

View Full Version : Extend: how to call parent's superclass



dncpax
28 Mar 2011, 9:23 AM
So I'm extending a class, and redefining one of it's methods... and I need to call it's parent superclass method... in fact calling my grandparent's method. Or my superclass.superclass method.

Explaining better.


myClass = Ext.extent(parentClass, {

overridenMethod: function() {
call parentClass.superclass.overridenMethod

some logic;
}
});

Is this even possible?

Thanks,
Duarte

crysfel
28 Mar 2011, 9:46 AM
Sure, this is possible.




myClass = Ext.extend(parentClass, {

overridenMethod: function() {
myClass.superclass.overridenMethod.call(this); // <--- this line do the magic

some logic;
}




The Ext.extend method create a "superclass" property that point to the prototype of the Superclass, this allow you to execute or read any method or property in the superclass (that is defined in it's prototype), if you want to execute a function you only need to call the "call" or "apply" method of the function that you want to execute, the "this" parameter is the scope where the "overridenMethod" is going to be executed.

I hope this helps.

Best regards

kohyea
28 Mar 2011, 11:53 AM
myClass.superclass.overridenMethod.call(this, arg1, arg2, ..., argN);
myClass.superclass.overridenMethod.apply(this, arguments);

I suggest using apply, it can pass the defaut arguments.

dncpax
30 Mar 2011, 1:43 AM
Thanks for your replies. But... I need to avoid the parent's method, and go to the grand-parents method... so, I want to avoid calling myClass.superclass.method.apply, and instead call myClass.superclass.superclass.method.apply.

superclass.superclass is undefined.

Now I work around this by using the name of the superclass:
MySuperClass.superclass.method.call or apply

But I would prefer to use an alternative that avoids hardcoding the name of the superclass.

Duarte

ssayen
23 Oct 2012, 2:04 AM
Hello Duarte

pretty old post, but I have exactly the same issue.

1 - Are you still alive ? ;)
2 - Did you find a clean way to do this ?

Thanks
Laurent

dncpax
23 Oct 2012, 2:59 AM
Hi there, yes I'm still breathing! ;)

I did not solve it. Just had to hardcode the superclass name, and call its superclass. Like:


MyClass = Ext.extend(OriginalClass, {

OriginalClass.superclass.method.apply...



If you find another solution let me know...

Duarte

ssayen
23 Oct 2012, 3:22 AM
Hey Duarte, glad you're fine ;)
Ok, I do it the same way.

Thanks anyway, I'll post here if I find anything else.

Laurent

Brian.linder
3 Dec 2012, 7:00 AM
I ran into this when you override an xtype, and then override that xtype's method but still want to callParent to get the ancestor functionality without the overridden method's functionality. It looks like Ext 4.1.3 and touch 2.1.0 have callSuper for this purpose. If you don't want to upgrade, you can use the following override to add this functionality until you do:



Ext.define('Ext.overrides.Base', {
override: 'Ext.Base',

/**
* TODO: REMOVE WHEN SENCHA TOUCH UPGRADED TO 2.1+
*/
callSuper: function(args) {
var method,
superMethod = (method = this.callSuper.caller) && ((method = method.$owner ? method : method.caller) && method.$owner.superclass[method.$name]);

if (!superMethod) {
method = this.callSuper.caller;
var parentClass, methodName;

if (!method.$owner) {
if (!method.caller) {
throw new Error("Attempting to call a protected method from the public scope, which is not allowed");
}

method = method.caller;
}

parentClass = method.$owner.superclass;
methodName = method.$name;

if (!(methodName in parentClass)) {
throw new Error("this.callSuper() was called but there's no such method (" + methodName +
") found in the parent class (" + (Ext.getClassName(parentClass) || 'Object') + ")");
}
}

return superMethod.apply(this, args || noArgs);
}
});

ronaldploeger
18 Sep 2013, 11:55 PM
One can use this.callParent(argumentsArray)


Ext.define('xxx', {
extend : 'Ext.data.Connection',
singleton : true,
request : function(options) {
// your own code here
this.callParent(arguments);
// or this.callParent([options]);
// your own code here
}
});

Note:

1) One can not use "use strict" because "callParent" needs to be able to access the "caller" property of the function.
2) You need to pass the arguments as an array because "callParent" uses the "apply" function