Javascript up() 和 down() 与 Ext.getCmp()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11872261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:28:06  来源:igfitidea点击:

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

javascriptextjsextjs4extjs4.1

提问by Expert wanna be

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

我很困惑我需要将哪一个用于 up() down() 和 Ext.getCmp(ID) 之间的 grep 对象。

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

对我来说,定义 ID 对象并通过 Ext.getCmp('ID') 检索对象更容易,代码看起来更干净。

For example:

例如:

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

which one is better for performance?

哪个性能更好?

回答by dbrin

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.

使用 ID 和 getCmp 查找组件存在严重的问题。主要问题是您不能安全地重用组件,因为在大多数情况下,标记会创建具有重复 ID 的 HTML 元素,这是无效的 HTML。此外,当您有两个具有相同 ID 的组件时,您将遇到难以跟踪的意外行为,因为框架将无法获得对您的组件的正确引用。

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.

有几个关于此的博客和论坛帖子,以及 J. Garcia 的视频,涵盖了这个主题。使用 getCmp 的建议方法仅用于调试,切换到其他方法(up、down、refs、itemIdComponentQuery)用于生产就绪代码。

回答by Izhaki

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.

Ext.getCmp()在内部使用哈希映射,因此它的发光速度很快,几乎与基于键检索数组中的值一样快。

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

.up()并且down()使用遍历组件层次结构来实现,这是一个较慢的过程。

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

但是up()down()使用选择器,因此他们还可以选择类,而不仅仅是 id。

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.

您只需要记住在各种情况下您可能想要使用up()or down(),例如当有多个控件的通用处理程序时,或者当您所追求的组件由框架自动生成时,您不能给它一个标识自己。