id vs. itemId
In Ext JS 2.x if you need to access any component anywhere in your code and you don’t have an object reference available, you have to assign that component an id
and later pass it to Ext.ComponentMgr.getCmp()
method (or its alias Ext.getCmp()
) to retrieve the reference to the component. Since Ext JS uses a global MixedCollection to store components’ id’s, you must use unique id for every component that you create. While it has worked for most developers, it is often hard to keep track of used ids, especially if you are a member of a bigger team that works on one project.
In Ext JS 3.0 an itemId
can be used as an alternative way to get a reference to a component when no object reference is available. Instead of using an id
with Ext.getCmp()
, use itemId
with Ext.Container.getComponent()
which will retrieve itemId’s or id’s. Since itemId’s are an index to the container’s internal MixedCollection, the itemId
is scoped locally to the container, avoiding potential conflicts with Ext.ComponentMgr
which requires a unique id.
var c = new Ext.Panel({ height: 300, renderTo: document.body, items: [{ itemId: 'p1', title: 'Panel 1', height: 150 }, { itemId: 'p2', title: 'Panel 2', height: 150 }] }); var p1 = c.getComponent('p1'); // not the same as Ext.getCmp() var p2 = p1.ownerCt.getComponent('p2'); // reference via a sibling
Great Example! thanks
Very useful feature. Thanks for sharing!
i didn’t know about this, thanks for share 😉
The ‘itemId’ feature was also available in the 2.x branch, but was not used by many. You could consider the ‘ref’ config that came out with 3.x to be its easier to use replacement.
Thanks for the comment! I did not know ‘itemId’ existed in 2.x. Anyways, you’re absolutely right about ‘ref’, I am using it everywhere in my code now. I don’t use ‘itemId’ at all, just don’t see a need. I have a post about ‘ref’ too: https://elishnevsky.wordpress.com/2009/05/18/using-ref-config-option-in-ext-js-3-0/ 🙂
Great example. Very succinct and clear.