View Full Version : Instantiating classes - this way or that way?

20 May 2011, 4:09 AM
Hi, I've seen the instantiating of a class done in 2 ways. eg

1) (using xtypes) - items: [ {xtype: 'window', id: 'myWindow', ....} ]
2) items:[ this.myWindow = new Ext.Window({ .... }) ]

I prefere 2 because i can refer to the component by going 'this.myWindow' afterwards, as opposed to 'Ext.getCmp('myWindow')'

Is there a downside to using 2? Is one way better than the other? I've read somewhere that you should always use xtypes where you can - why? does method 2 break this rule?

21 May 2011, 1:43 PM
check http://www.sencha.com/learn/Tutorial:Xtype_defined

It's for performance. Using xtypes will render a component when the user needs to see it, rather than creating them when the application is loaded.

Try to avoid using ids, check out "ref" and "itemId".

22 May 2011, 9:50 PM
Hi drian, thanks for your reply. It makes sense, but what about this...

i've got a panel instantiated using an xtype, but one of its items is declared statically,
so that I can simply reference it using ' this.myWindow' as opposed to getting it by itemId or Id.
Is this acceptable/recommended? (sorry, the example is very arbitary)

xtype: 'panel',
title: 'My panel',
items:[ this.myWindow = new Ext.Window({ .... }) ],
listeners: {
someEvent: function(){this.myWindow.show();}