问题
I am new to Extjs and using Extjs v4.2.0. To find an object I can use either Ext.getCmp(id) or Ext.ComponentQuery.query(attribute).Which one is better to use and faster?
回答1:
The best is to avoid using any of this two directly. Ext.getCmp
is especially considered bad "code smell". You should strive to organize your code in such a way that you don't need them.
Component queries are elegant, but you should use them either from a parent container (thus reducing the research tree and allowing you to leverage relative itemId), or from a controller. It is my guess that controllers are the true reason why component queries have been added to Ext4.
回答2:
getCmp
will be faster, since it's a simple hash lookup. When you use query
, it has to parse and then execute the query, so for a simple id only, getCmp
is better.
However, be wary of using id's, since they need to be globally unique. It's only a good idea to use them in cases when you know there will only be one, for example a login window, or your main app container.
来源:https://stackoverflow.com/questions/17871798/ext-getcmpid-or-ext-componentquery-queryattribute