javascript - 类定义中的 Extjs itemId

标签 javascript extjs extjs4

在类定义(而不是实例化)上配置 itemId 是一种不好的做法吗?

是否有一些官方文档支持它,或者它只是一个意见问题?

或者,也许我缺少一些逻辑,可以清楚地表明这是一种不好的做法。

Ext.define('SomeApp.view.SomeFolder.MySpecialComponent',{
    extend: 'Ext.panel.Panel',
    itemId: 'specialComponent'
    // ...
});

因为我知道如果有超过 1 个实例并且我使用 itemId 作为选择器我会得到两个实例。但是假设我知道我一次不会有超过 1 个实例,并且假设实例化可以发生在 3 个不同的地方,我不想在这 3 个不同的地方添加 itemId,我当然不想希望这些 itemId 不同。

那么官方有关于在class定义中使用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

特别是考虑到这个事实,在我看来直接在类定义中指定 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'
        }];
    

关于javascript - 类定义中的 Extjs itemId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29944305/

相关文章:

javascript - Extjs 6-获取网格面板单元格值

function - Ext.XTemplate 中的内联变量

javascript - 检索商店记录

javascript - 需要 extjs4 增强功能的高级 VType 密码验证

javascript - 分机 Js : How to load a store from List<String>

javascript - JavaScript 中的 Oauth

javascript - 将杏仁与 grunt-contrib-requirejs 一起使用

javascript - 如何修复 Farbtastic 忽略滚动条的位置

javascript - ExtJs。如何使 "Selected Item in the gridPanel"成为孤儿?

JavaScript 无法选择复选框