views:

2404

answers:

5

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?

+1  A: 

You could use this little known Javascript feature (arguments.callee):

MyApp.MyPanel = Ext.extend(Ext.Panel, {
    constructor: function() {
        // Do your thing
        this.thing = 1;

        // Call super
        arguments.callee.superclass.constructor.apply(this, arguments);
    }
});

see MDC documentation

Edit: Actually, this isn't going to work with initComponent because it isn't the constructor. I always override the constructor, personally (despite what Ext JS examples suggest). Will continue to think about this one a bit.

neonski
Callee is deprecated.
Jabe
JavaScript 1.4: Deprecated callee as a property of Function.arguments, retained it as a property of a function's local arguments variable. Not the same thing!
neonski
Ah, sorry, you are right. I had it confused with the completely deprecated 'caller'. Removing callee would be quite a mess.
Jabe
+1  A: 

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 ...

Jabe
Thanks, it indeed doesn't work for multiple levels of inheritance hierarchy. FYI: nothing has changed in this regard in 3.0.3.
Rene Saarsoo
A: 

Guy what u write is very bad, this kind of use will break what u want do in case deep inheritance. first method is ok.

peter
A: 

nionski is right

http://www.extensions.extjs.com/learn/Manual:Intro:Inheritance

Justin
+1  A: 

Here's a pattern I use, and been meaning to blog about it for a while.

Ext.ns('MyApp.MyPanel');

MyApp.MyPanel = (function(){
  var $this = Ext.extend(Ext.Panel, {
    constructor: function() {
        // Using a 'public static' value from $this
        // (a reference to the constructor)
        // and calling a 'private static' method
        this.thing = $this.STATIC_PROP + privateStatic();
        // Call super using $super that is defined after 
        // the call to Ext.extend
        $super.constructor.apply(this, arguments);
    },
    initComponent: function() {
        $super.initComponent.call(this);
        this.addEvents([Events.SOMETHING]); // missing docs here
    }
  });
  var $super = $this.superclass;

  // This method can only be accessed from the class 
  // and has no access to 'this'
  function privateStatic() {
    return "Whatever";
  }

  // You can create public static properties like this
  // refer to Events directly from the inside
  // but from the outside somebody could also use it as
  //  MyApp.MyPanel.Events.SOMETHING
  var Events = $this.Events = {
      SOMETHING: 'something'
  }

  return $this;
})();

MyApp.MyPanel.STATIC_STRING = 10;

//Later somewhere
var panel = new MyApp.Mypanel();
panel.on(MyApp.Mypanel.Events.SOMETHING, callback);

There are a lot of features you get using this pattern, but you don't have to use all of them

Juan Mendes