Ext.getCmp(id) or Ext.ComponentQuery.query(attribute)

痴心易碎 提交于 2019-12-08 05:53:35

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!