PDA

View Full Version : Is not a constructor error



simon
9 Jun 2016, 2:11 AM
When I would like to create new component:



this.basketList = new SM.Basket.List({
flex: 1
});


I get error:
Uncaught TypeError: SM.Basket.List is not a constructor.

What should I change? Is there something with namespaces?

https://fiddle.sencha.com/#fiddle/1bpp

(SM.Basket.List is in basketList file which is included before app.js)

tristan.lee
13 Jun 2016, 8:56 PM
Your assignment SM.Basket = Ext.extend(Ext.Panel, {...}); is changing SM.Basket namespace containing your List class to an Ext.Panel, so List no longer exists.

simon
14 Jun 2016, 5:40 AM
If I have:


SM.Basket.List = Ext.extend(Ext.Panel, {...});

SM.Basket = Ext.extend(Ext.Panel, {
constructor: function (cfg) {
this.basketList = new SM.Basket.List({});
}
...
});

So, first child class is declared, then parent.
It produce mentioned error. But if i change order, than it works:




SM.Basket = Ext.extend(Ext.Panel, {
constructor: function (cfg) {
this.basketList = new SM.Basket.List({});
}
...
});

SM.Basket.List = Ext.extend(Ext.Panel, {...});


But it is not true for similar case:



SM.Basket.ListToolbar = Ext.extend(Ext.Toolbar, {...});

SM.Basket.List = Ext.extend(Ext.Panel, {
initComponent: function () {
this.add(new SM.Basket.ListToolbar({}));
}
});


Here it works even if child class is defined before parent one. The only difference is that in this example the "SM.Basket.ListToolbar"
is added on initComponent, while "SM.Basket.List" is added to SM.Basket inside constructor.

I'm not sure if i understand this. Can you explain a little bit more, please?
Why defining class order sometimes is important and sometimes not.

tristan.lee
14 Jun 2016, 6:42 AM
Let's simplify the first case. This code is what is giving you trouble:


SM.Basket.List = Ext.extend(Ext.Panel, {...});

SM.Basket = Ext.extend(Ext.Panel, {
constructor: function (cfg) {
this.basketList = new SM.Basket.List({});
}
...
});

I've broken this down into a basic Object which represents when Ext creates the namespace. You can run this example and observe in the console that the List constructor is called the first time, then when you define the SM.Basket class, you are now changing SM.Basket from an Object that contains other properties (such as your List class) to a Function. Now SM.Basket.List no longer exists.

1c0d

When you do it in the different order (like below), SM.Basket gets defined first, and then the assignment of SM.Basket.List now makes List a property of the SM.Basket class.

1c0f

simon
14 Jun 2016, 11:33 PM
Thank you Tristan, you are great.
This is the best possible answer, now everything is clear :)