Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better way to call superclass method in ExtJS

All the ExtJS documentation and examples I have read suggest calling superclass methods like this:

MyApp.MyPanel = Ext.extend(Ext.Panel, {   initComponent: function() {     // do something MyPanel specific here...     MyApp.MyPanel.superclass.initComponent.call(this);   } }); 

I have been using this pattern for quite some time and the main problem is, that when you rename your class then you also have to change all the calls to superclass methods. That's quite inconvenient, often I will forget and then I have to track down strange errors.

But reading the source of Ext.extend() I discovered, that instead I could use the superclass() or super() methods that Ext.extend() adds to the prototype:

MyApp.MyPanel = Ext.extend(Ext.Panel, {   initComponent: function() {     // do something MyPanel specific here...     this.superclass().initComponent.call(this);   } }); 

In this code renaming MyPanel to something else is simple - I just have to change the one line.

But I have doubts...

  • I haven't seen this documented anywhere and the old wisdom says, I shouldn't rely on undocumented behaviour.

  • I didn't found a single use of these superclass() and supr() methods in ExtJS source code. Why create them when you aren't going to use them?

  • Maybe these methods were used in some older version of ExtJS but are deprecated now? But it seems such a useful feature, why would you deprecate it?

So, should I use these methods or not?

like image 448
Rene Saarsoo Avatar asked Nov 12 '09 12:11

Rene Saarsoo


2 Answers

I think this is solved in ExtJS 4 with callParent.

Ext.define('My.own.A', {     constructor: function(test) {         alert(test);     } });  Ext.define('My.own.B', {     extend: 'My.own.A',      constructor: function(test) {         alert(test);          this.callParent([test + 1]);     } }); 
like image 55
tykovec Avatar answered Oct 04 '22 02:10

tykovec


Yes indeed, supr() isn't documented. I've been looking forward to using it in ExtJS 3.0.0 (an Ext staff member replied in the forums, they had added it in that version), but it seems horribly broken.

It currently does not traverse the inheritance hierarchy, but rather go up one level, then gets stuck on this level, loops endlessly and blows up the stack (IIRC). So, if you have two or more supr() in a row, your app will break. I have not found any useful information on supr() in neither the docs nor the forums.

I don't know about the maintenance releases 3.0.x, since I did not get an support license ...

like image 35
Jabe Avatar answered Oct 04 '22 02:10

Jabe