将 css class 添加到具有特定名称的特定 x 类型

Adding css class to specific xtype with certain name's

在我的 ExtJS 应用程序中,我希望能够在我的整个过程中将特定的 class 名称添加到 xtype(无论 xtype 是按钮、复选框、文本字段、网格面板项等)如果 itemId/name 与我拥有的 ID 的 list/dictionary 匹配,则应用程序。

这可能吗?

所以让我们假装这是我的代码在定义元素时的当前样子

{
    xtype: 'button',
    text: 'some text',
    id:'elementA',
    cls: 'mycustomclass'
},
{
    xtype: 'checkbox',
    itemId: 'chkBoxC',
    id:'elementC',
    cls: 'mycustomclass'    

},
{
    xtype: 'button',
    text: '',
    id:'elementB'
}

所以在上面的片段中我想要发生的是(伪代码)

if (xtype.items.id == 'elementA' or xtype.items.id= 'elementC')
   add class = "mycustomclass"

我希望能够避免在我的所有文件中散布 mycustomclass 并继续保持在下方查看。 我可以在哪里覆盖?

{
    xtype: 'button',
    text: 'some text',
    id:'elementA',

},
{
    xtype: 'checkbox',
    itemId: 'chkBoxC',
    id:'elementC'
},
{
   xtype: 'button',
   text: '',
   id:'elementB'

}

您可以覆盖所有类型的 initComponent

例如

Ext.define('OverrideButton', {
    override: 'Ext.button.Button',
    initComponent: function () {
        if(this.id == 'elementA' or this.id == 'elementB') {
            this.cls = 'mycustomclass';
        }
        this.callParent(arguments);
    }
});

Ext.define('OverrideCheckBox', {
    override: 'Ext.form.field.Checkbox',
    initComponent: function () {
        if(this.id == 'elementA' or this.id == 'elementB') {
            this.cls = 'mycustomclass';
        }
        this.callParent(arguments);
    }
});

您可以尝试仅覆盖 Ext.Component,它是复选框、字段等的基础(extjs 7.1 示例)

Ext.define('OverrideComponent', {
    override: 'Ext.Component',
    initComponent: function () {
        if(this.id == 'elementA' or this.id == 'elementB') {
            this.cls = 'mycustomclass';
        }
        var me = this,
            width = me.width,
            height = me.height;

        // If plugins have been added by a subclass's initComponent before calling up to here
        // (or any components that don't have a table view), the processed flag will not have been
        // set, and we must process them again.
        // We could just call getPlugins here however most components don't have them so prevent
        // the extra function call.
        if (me.plugins && !me.plugins.processed) {
            me.constructPlugins();
        }

        me.pluginsInitialized = true;

        // this will properly (ignore or) constrain the configured width/height to their
        // min/max values for consistency.
        if (width != null || height != null) {
            me.setSize(width, height);
        }

        if (me.listeners) {
            me.on(me.listeners);
            me.listeners = null; // change the value to remove any on prototype
        }

        if (me.focusable) {
            me.initFocusable();
        }
        
    }
});