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

时间:2022-10-15 07:37:37

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?

哪一个更好的表现?

2 个解决方案

#1


34  

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,itemId和ComponentQuery)。

#2


15  

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()或down()的各种情况,比如当有一个用于多个控件的公共处理程序时,或者你所使用的组件是由框架自动生成的时候所以你不能自己给它一个id。

#1


34  

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,itemId和ComponentQuery)。

#2


15  

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()或down()的各种情况,比如当有一个用于多个控件的公共处理程序时,或者你所使用的组件是由框架自动生成的时候所以你不能自己给它一个id。