Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EXTJS - How to verify if element exists?

Tags:

extjs

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');
} 
like image 322
Victor Avatar asked Jul 19 '10 16:07

Victor


1 Answers

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.

EDIT: Several years have passed since this original answer, and nowadays getCmp is generally frowned upon as a code smell and should typically be avoided in applications (promotes global references, which generally indicate poor design when they are required). It's typically better to use controller references (if using MVC) or the various ComponentQuery or Container methods to reference related components (e.g. down, child, getComponent, etc.)

like image 149
Brian Moeskau Avatar answered Sep 23 '22 10:09

Brian Moeskau