up() and down() versus Ext.getCmp()

梦想与她 提交于 2019-12-17 07:25:10

问题


I'm very confused which one I need to use for grep object between up() down() and Ext.getCmp(ID).

For me, it is easier that define ID to object and retrieve the object by Ext.getCmp('ID') and the code looks more clean.

For example:

console.log(this.up('panel').up('panel').down('grid'));
console.log(Ext.getCmp('myPanel'));

which one is better for performance?


回答1:


There are severe gotchas with using IDs and getCmp to find your components. The primary issue is that you can not reuse the component safely because in most cases the markup will create HTML elements with duplicate IDs, which is invalid HTML. Additionally, when you have two components with the same ID you will run into unexpected behavior that is difficult to track because the framework will not get a correct reference to your components.

There are several blog and forum posts on this, as well as a video by J. Garcia covering this topic. The suggested way to use getCmp is for debugging only, switching to other methods (up, down, refs, itemId, and ComponentQuery) for production-ready code.




回答2:


Ext.getCmp() uses a hash map internally, so it is lighting fast, nearly as fast as retrieving the value in an array based on a key.

.up() and down() are implemented using a traversal of the component hierarchy, which is a slower process.

But up() and down() use selectors, so they can also select classes, not just ids.

You just need to remember that are various situations where you might want to use up() or down(), like when there's a common handler for a multitude of controls, or when the component you're after was auto-generated by the framework so you cannot give it an id yourself.



来源:https://stackoverflow.com/questions/11872261/up-and-down-versus-ext-getcmp

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