View Full Version : Container.getComponent fails to lookup by id if itemId has been set

29 Aug 2011, 12:03 PM
I ran into this interesting bug when working with a segmentedbutton that contains buttons set with itemId. When this happens, the segmentedbutton code could not correctly set the pressed state of the buttons in contained. After tracking it down, it ended up being a problem where Container.getComponent (in Ext.lib.Container) fails to find contained components by .id when the components have .itemId set as well.

The issue seems to be that the this.items MixedCollection is indexed by Container.getComponentId which defaults to itemId and thus makes the Container.getComponent lookup fail when indexed only by .id.

Here is the fix that I am currently using. It isn't pretty, but it works well.

Ext.override(Ext.lib.Container, {
getComponent : function(comp) {
var ret_comp, x, item;

if (Ext.isObject(comp)) {
comp = comp.getItemId();

ret_comp = this.items.get(comp);

// If not found, try a slower more accurate lookup
if(undefined === ret_comp) {
if(undefined === comp) { return undefined; }

for(x = 0; x < this.items.getCount(); x++) {
item = this.items.getAt(x);
if((comp === item.itemId) || (comp === item.id)) {
return item;

return ret_comp;