PDA

View Full Version : Ext.extend defined in Class does not work like the old Ext.extend defined in Ext



chunkT
29 Nov 2011, 12:04 AM
Hi all,

i try to upgrade to ExtJS 4.* and have figured out that the extend mechanism for classes does not work as aspected when using Ext.extend; I am aware of the fact that this function is deprecated and that i should use Ext.define but i am curios if the following behavior is intentional or not.

Previous to ExtJS 4 i had some classes defined the following way:



MyClass = function(){
console.log('MyClass.constructor');
this.addEvents('dosomething');
MyClass.superclass.constructor.call(this);
};

Ext.extend(MyClass, Ext.util.Observable, {
doSomething : function(){

}
});
new MyClass() //logs MyClass.constructor

//creating a new SubClass from MyClass

MySubClass = Ext.extend(MyClass, {
constructor : function(){
console.log('MySubClass.constructor');
MySubClass.superclass.constructor.call(this);
}
});

new MySubClass()
//logs MySubClass.constructor then MyClass.constructor in Ext < 4
//logs MySubClass.constructor in Ext >= 4


Since ExtJS 4 the constructor of MyClass is not called anymore.
Is this considered a bug or a feature? :]

Best,
chunk

burnnat
29 Nov 2011, 5:53 AM
The functionality you're looking for does exist in ExtJS 4. In fact, calling superclass constructors and methods is even easier than it was previously - all you have to do is use this.callParent() (http://docs.sencha.com/ext-js/4-0/#%21/api/Ext.Base-method-callParent) and pass an array of arguments to use. Here's a modified version of your example that will work in ExtJS 4:

Ext.define('MyClass', {
constructor: function() {
console.log('MyClass.constructor');
}
});

Ext.define('MySubClass', {
extend: 'MyClass',

constructor: function() {
console.log('MySubClass.constructor');
this.callParent(arguments);
}
})

Ext.create('MySubClass');