tags:

views:

92

answers:

3

Hi all, I need to know if a boxComponent exists in a ext formPanel in order to take some actions... Is there some way to know that? something like this:

if(getElementById("boxId") != 'undefined' ){
    alert('exists');
} 
A: 

You can use Ext.get('boxId'). It returns null if the object doesn't exist and returns an Ext.Element object.

Using getElementById would probably be much faster though. Do you have any specific objection against it?

WoLpH
Ext.get is a keyed lookup into a hash table, so it is basically just as fast as getElementById (and has the benefit of returning an Ext.Element instead of a DOM node, assuming that's the ultimate goal).
bmoeskau
That is only a benefit the next time you execute it. The first time will still be slow. Then again, if speed is your goal than you probably wouldn't be using Ext JS :)
WoLpH
True. Practically speaking though, if you are using Ext to begin with, there isn't much reason to go outside of its Element abstraction. And by "faster" you're still only talking about a millisecond or two these days.
bmoeskau
First of all thanks everyone. The problem has being solved.I've tried all the ways of doing it. Good to know the diference between the way of finding the elements and the several methods to do so.Thx
Victor
A: 
var boxComponent = formPanel.findById('boxId');
if(boxComponent != null) {
     alert('exists');
}

I think use of Ext.get and Ext.getCmp is discouraged.

BrennaSoft
No, Ext.get and Ext.getCmp are not discouraged. They are keyeed hash table lookups, so they are actually faster than findByX methods that loop recursively through child elements looking for a match. Also, you don't always have a reference to a container, so in such cases you'd have to use get or getCmp.
bmoeskau
Ext.get and Ext.getCmp are the cornerstones of Extjs -1 in spirit :)
DRL
+3  A: 

The common pattern that most people use is this:

var myBoxCmp = Ext.getCmp('cmpId');
if(myBoxCmp){
    myBoxCmp.doSomething();
}

Same thing for Elements:

var el = Ext.get('elId');
if(el){
    el.doSomething();
}

You can also use methods like Container.findById, but if you have an id (assuming it is unique, which it should be) just use getCmp.

bmoeskau