class 定义中的 Extjs itemId
Extjs itemId in class definition
在 class 定义(而不是实例化)上配置 itemId 是一种不好的做法吗?
是否有一些官方文档支持它或者它只是一个意见问题?
或者可能是我遗漏了一些逻辑,这表明这是一种不好的做法。
Ext.define('SomeApp.view.SomeFolder.MySpecialComponent',{
extend: 'Ext.panel.Panel',
itemId: 'specialComponent'
// ...
});
因为我知道如果有超过 1 个实例并且我使用 itemId 作为选择器我会得到两个实例。但是假设我知道我一次不会有超过 1 个实例,并且假设实例化可以发生在 3 个不同的地方,我不想在这 3 个不同的地方添加 itemId,我当然不想希望这些 itemId 不同。
那么在class定义处使用itemId配置有官方的姿势吗?
官方姿势:
An itemId can be used as an alternative way to get a reference to a component when no object reference is available. Instead of using an id with Ext.getCmp, use itemId with Ext.container.Container.getComponent which will retrieve itemId's or id's. Since itemId's are an index to the container's internal MixedCollection, the itemId is scoped locally to the container -- avoiding potential conflicts with Ext.ComponentManager which requires a unique id.
http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.AbstractComponent-cfg-itemId
因此您可以像这样使用 itemId 不被禁止。
至少有两个原因,为什么这是一个不好的做法:
- 一段时间后您可以忘记此行为
- 您团队中的某个人或稍后将使用您的代码的其他程序员可能会使用此组件并面临冲突。
您可以做出的更好的解决方案是将 itemId
定义移动到具有默认值的配置块。在那种情况下,其他用户可以轻松更改此 属性.
来自 Ext.AbstractComponent.itemId 的文档:
An itemId can be used as an alternative way to get a reference to a component when no object reference is available. Instead of using an id with Ext.getCmp, use itemId with Ext.container.Container.getComponent which will retrieve itemId's or id's. Since itemId's are an index to the container's internal MixedCollection, the itemId is scoped locally to the container -- avoiding potential conflicts with Ext.ComponentManager which requires a unique id.
由于itemId
作为索引,所以在容器内必须是唯一的。如果您将具有相同 itemId 的任何组件的两个实例添加到同一个容器中,第二个实际上会覆盖第一个。
您可以在 fiddle 中观察到此行为:https://fiddle.sencha.com/#fiddle/m2n
特别是考虑到这个事实,在我看来直接在class定义中指定itemId并没有什么意义,因为:
- 如果您想将此组件的 1 个以上实例添加到同一个容器中,它根本无法工作(除非您在实例化时再次覆盖 itemId)。
如果不是这种情况,那么您可能要考虑根本不指定 itemId。相反,您可以通过其 xtype:
获取实例
// assuming the xtype is 'specialcomponent'
container.query('specialcomponent')
您实际上会在实例化时隐藏 itemId,从而更难理解它的来源
var ct = Ext.create('Ext.container.Container', {
items: [{
// not clear that this will have itemId 'specialComponent'
xtype: 'specialcomponent'
},{
xtype: 'panel',
itemId: 'somePanel'
}];
添加到@matt 和@Nikolay 的回答中 -
Is it a bad practice to have the itemId configuration on the class
definition (instead of instantiation)?
最好不要。
确实可以存在多个具有相同 itemId 的组件,只要它们位于不同的容器中即可(即使子容器也可以包含与父容器中存在的组件具有相同 itemId 的组件) .然而,问题在于我们必须谨慎对待基于唯一 itemId 的逻辑。如果我们对所有实例都遵循唯一的 itemId,那么我们可以轻松地仅使用唯一的 itemId 进行组件查询,并确保我们想要的组件是始终返回的组件——在我们引入重复的 itemId 的情况下不会出现这种情况在系统中,我们必须始终确保传递了正确的查询选择器。
除非,绝对绝对不可避免地需要。我会选择唯一的 itemId。
在 class 定义(而不是实例化)上配置 itemId 是一种不好的做法吗?
是否有一些官方文档支持它或者它只是一个意见问题?
或者可能是我遗漏了一些逻辑,这表明这是一种不好的做法。
Ext.define('SomeApp.view.SomeFolder.MySpecialComponent',{
extend: 'Ext.panel.Panel',
itemId: 'specialComponent'
// ...
});
因为我知道如果有超过 1 个实例并且我使用 itemId 作为选择器我会得到两个实例。但是假设我知道我一次不会有超过 1 个实例,并且假设实例化可以发生在 3 个不同的地方,我不想在这 3 个不同的地方添加 itemId,我当然不想希望这些 itemId 不同。
那么在class定义处使用itemId配置有官方的姿势吗?
官方姿势:
An itemId can be used as an alternative way to get a reference to a component when no object reference is available. Instead of using an id with Ext.getCmp, use itemId with Ext.container.Container.getComponent which will retrieve itemId's or id's. Since itemId's are an index to the container's internal MixedCollection, the itemId is scoped locally to the container -- avoiding potential conflicts with Ext.ComponentManager which requires a unique id.
http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.AbstractComponent-cfg-itemId
因此您可以像这样使用 itemId 不被禁止。
至少有两个原因,为什么这是一个不好的做法:
- 一段时间后您可以忘记此行为
- 您团队中的某个人或稍后将使用您的代码的其他程序员可能会使用此组件并面临冲突。
您可以做出的更好的解决方案是将 itemId
定义移动到具有默认值的配置块。在那种情况下,其他用户可以轻松更改此 属性.
来自 Ext.AbstractComponent.itemId 的文档:
An itemId can be used as an alternative way to get a reference to a component when no object reference is available. Instead of using an id with Ext.getCmp, use itemId with Ext.container.Container.getComponent which will retrieve itemId's or id's. Since itemId's are an index to the container's internal MixedCollection, the itemId is scoped locally to the container -- avoiding potential conflicts with Ext.ComponentManager which requires a unique id.
由于itemId
作为索引,所以在容器内必须是唯一的。如果您将具有相同 itemId 的任何组件的两个实例添加到同一个容器中,第二个实际上会覆盖第一个。
您可以在 fiddle 中观察到此行为:https://fiddle.sencha.com/#fiddle/m2n
特别是考虑到这个事实,在我看来直接在class定义中指定itemId并没有什么意义,因为:
- 如果您想将此组件的 1 个以上实例添加到同一个容器中,它根本无法工作(除非您在实例化时再次覆盖 itemId)。
如果不是这种情况,那么您可能要考虑根本不指定 itemId。相反,您可以通过其 xtype:
获取实例// assuming the xtype is 'specialcomponent' container.query('specialcomponent')
您实际上会在实例化时隐藏 itemId,从而更难理解它的来源
var ct = Ext.create('Ext.container.Container', { items: [{ // not clear that this will have itemId 'specialComponent' xtype: 'specialcomponent' },{ xtype: 'panel', itemId: 'somePanel' }];
添加到@matt 和@Nikolay 的回答中 -
Is it a bad practice to have the itemId configuration on the class definition (instead of instantiation)?
最好不要。 确实可以存在多个具有相同 itemId 的组件,只要它们位于不同的容器中即可(即使子容器也可以包含与父容器中存在的组件具有相同 itemId 的组件) .然而,问题在于我们必须谨慎对待基于唯一 itemId 的逻辑。如果我们对所有实例都遵循唯一的 itemId,那么我们可以轻松地仅使用唯一的 itemId 进行组件查询,并确保我们想要的组件是始终返回的组件——在我们引入重复的 itemId 的情况下不会出现这种情况在系统中,我们必须始终确保传递了正确的查询选择器。 除非,绝对绝对不可避免地需要。我会选择唯一的 itemId。