7

I am looking for a way to access components / field that are either in the same items array as the accessing one or even only in a same parent items array (the last one is just a option).

In ExtJS3 this was easy by simply defining a ref in the owner container but I didn't found anything like that in ExtJS4.

I know that I can use Ext.ComponentQuery() or the shortcuts up() / down() or even Ext.getCmp() but they are all not what I am looking for, cause they just executes a bunch of code while the ref was such an easy Way to do things. Yes, I am aware of the fact that using a ComponentQuery is much more fail safe than the use of hard coded references. But I just want to know if there are some other ways to do this.

sra
  • 23,820
  • 7
  • 55
  • 89
  • I use refs all the time in my controllers: http://docs.sencha.com/ext-js/4-1/#!/api/Ext.app.Controller-cfg-refs – Tim Wickstrom Nov 02 '12 at 16:24
  • Well I don't mentioned Controller cause I don't use them. I know that they know have a ref system, but that is not what I am talking about... So that did not help at all – sra Nov 02 '12 at 18:48

2 Answers2

11

Alternately, for your case of getting the next element in a container, you can use the nextSibling or prevSibling. All components have these methods. It would be a little less walking around the DOM structure. They also allow for a selector argument.

They are described in the docs here.

egerardus
  • 11,316
  • 12
  • 80
  • 123
  • +1 and thanks for the reply and yes this seems to be fitting for my needs. And even if @dbrin answers contains nearly the same I will mark his answer as correct (he explained it along with an example) cause I can't tell which one of you answered first. – sra Nov 05 '12 at 11:02
6

Here are some tricks I have used:

//lookup by name
formPanel.getForm().findField('state'); 

//lookup using nextSibling/prevSibling in a fieldset or fieldcontainer
myField.ownerCt.nextSibling('textfield[fieldLabel=Description]')

Here fieldLabel property is used to narrow down field selection but you can use ANY property at all. So if you construct a field with a property ref you can then use it to select your field similar how you would use it in a ComponentQuery .

dbrin
  • 15,525
  • 4
  • 56
  • 83
  • thanks for accepting! I'll +1 geronimos answer as well for being first. – dbrin Nov 05 '12 at 18:44
  • Thanks! You guys seen jsfiddle issues? http://meta.stackexchange.com/questions/153630/why-are-answers-with-jsfiddle-net-in-body-not-allowed – dbrin Nov 05 '12 at 19:08
  • I have found another way to access adjacent components which is exactly what I was looking for. Therefore I removed the 'correct answer' from you. Fell free to comment why my new approach my be wrong . It it comes out that both approaches nearly the same I recheck your answer. But for now the new one looks leaner to me. – sra Nov 20 '12 at 06:59