tags:

views:

983

answers:

2

I see there are lot's of examples in extjs where instead of actually creating extjs objects, an object literal with an xtype property is passed in.

What is this good for? Where is the performance gain (if that's the reason) if the object is going to be created anyway?

+2  A: 

xtypes in ext is not for performance. It is for easy to code. With help of xtype code becomes shorter, easy to read and understand, Especially if you are creating a Ext.form.FormPanel and it has many textareas, datefields, combos and so on... For example: it is useful when every datefield of form must have format like 'd.m.Y', you do not need to write separately format: 'd.m.Y' to every datefield, you just write to form config object defaults:{format:'d.m.Y'} ... there is many case when xtype shorters the code...

In conclusion: it is for easy to code...

Zango
forgot about lazy rendering. "Upper Stage" explained it well.
Zango
xtypes are more about performance and less about ease of coding: but they *do* lead to a more config-driven assembly of components, rather than the explicit (and ugly) "create-an-object in a var" approach.
Jonathan Julian
Yeah, the statement "xtype is not for performance" is just plain wrong. That's one of the primary purposes of it.
bmoeskau
+10  A: 

xtype is a shorthand way to identify particular components: 'panel' = Ext.Panel, 'textfield' = Ext.form.TextField, etc. When you create a page or a form, you may use these xtypes rather than instantiate objects. For example,

items: [{
   xtype: 'textfield',
   autoWidth: true,
   fieldLabel: 'something'
}]

Moreover, creating pages in this manner allows ExtJS to render lazily the page. This is where you see a "performance gain." Instead of creating a large number of components when the app loads, ExtJS renders components when the user needs to see them. Not a big deal if you have one page, but if you exploit tabs or an accordion, many pages are initially hidden and therefore the app will load more quickly.

Furthermore, you may create and register new components creating xtypes of your choosing. ExtJS will similarly render your components lazily.

You may also retrieve components by ID. Since your component (as well as the ExtJS components) may provide a bunch of nice behavior, it is sometimes convenient to search for and retrieve a component rather than a simple DOM element or node.

In short, xtypes identify components and components are a key aspect of ExtJS.

Upper Stage
I think you may have created a false dichotomy when you say that "instead of creating a large number of components when the app loads". I'm very curious to know whether or not you think that there is no other way to instantiate a component when it's needed and then destroy it when it's not.
RibaldEddie