View Full Version : This is bad, but why ?

12 Jul 2011, 10:44 AM
I've spent the last few hours reading all the threads and notes on components and factories in Ext.3x, e.g.:

* http://www.sencha.com/forum/archive/index.php/t-107512.html
* http://blog.extjs.eu/know-how/factory-functions-in-ext-extensions/
* http://tdg-i.com/364/abstract-classes-with-ext-js
* http://www.sencha.com/forum/showthread.php?54537-code-structure&p=259901

But, I'm still missing a "why". Here's my problem. The following code works, but everyone says it is a bad practice. Why ?

MY.myPanel = Ext.extend(Ext.grid.GridPanel, {
closeable: false,
border: true,
width: 350

12 Jul 2011, 10:23 PM
this isn't a bad approach.. in fact its the best way to code in ext js. If using an OOP concept is bad then it would never have been used so widely in every other language.
I read in one blog post by one of the deveopers of Ext Js that this was the suggested approach to extjs.

13 Jul 2011, 12:57 AM
Whether that code is bad depends on what do you think it does and what you want to do!

What it actually does is declare a new class MY.myPanel. This is a bad name for a class, which by convention have capitalised names, so it should be MY.MyPanel (and your namespace 'MY' should probably be 'My'). Camel-case like myPanel should be used for variables with object instances in them. That code doesn't actually make any panels, you would construct panel instances based on that class definition with

var myPanel1 = new MY.MyPanel({...});
var myPanel2 = new MY.MyPanel({...});

You can also just make your panel by directly configuring an Ext.Panel:

var otherPanel = new Ext.Panel({....});