【问题标题】:What are some good approaches for reusing parts of a config?重用配置部分的一些好方法是什么?
【发布时间】:2023-04-04 12:20:01
【问题描述】:

例如,我有几个共享公共列的网格(问题不仅限于列,任何配置对象)。目前我正在做这样的事情:

//columns.js
var columns = {"reusable1": {...}, "reusable2": {...}};

//grid views
Ext.define('MyApp.view.Grid', {
    columns: [
        {text:'inline column1'},
        columns.reusable1,
        columns.reusable2,
        {text:'inline column2'},
    ]
});

这可以完成工作,但是以类似方式工作的更好方法是什么(无需为每个配置块创建唯一的xtype,或修改constructorinitComponent 中的配置)。我只是希望能够像以前一样将其他可重用的配置块内联。

【问题讨论】:

    标签: extjs extjs6


    【解决方案1】:

    我认为大多数其他选项可能会降低可读性或看起来完全令人费解 - 但您可以覆盖基类并包含 "named" 列的配置。

    我假设当您说,“不在构造函数中修改配置”您指的是实现列的网格,而不是可以在其他地方整理的类似逻辑。

    Ext.define('App.override.grid.column.Column', {
        override: 'Ext.grid.column.Column',
    
        config: {
            name: null    
        },
    
        statics: {
            namedColumns: {
                reusable1: { /* ... */ },
                reusable2: { /* ... */ },
                // ...
            }
        },
    
        constructor: function(args){
            var _args = args || {},
                statics = Ext.grid.column.Column,
                defaults = statics.namedColumns[_args.name] || {};
            this.callParent([Ext.apply({}, _args, defaults)]);
        }
    });
    

    用法

    Ext.define('App.view.Grid', {
        columns: [
            { text: 'inline column1' },
            { name: 'reusable1'      },
            { name: 'reusable2'      },
            { text: 'inline column2' },
        ]
    });
    

    优势

    • 在项目结构中整理而不是存在于全球范围内。
    • 避免潜在的依赖问题,因为 Sencha CMD 将确保在声明任何网格之前存在覆盖(以及这些默认值)。
    • 命名配置更加灵活,可以巧妙地内联覆盖。
      例如 { name: 'reusable', flex: 2 /* superseding value */ }

    缺点

    • xtype 不能用作默认配置的一部分,除常规网格列外,仍需要内联指定。
      例如 { xtype: 'datecolumn', name: 'some-defaults' }

    » Fiddle

    【讨论】:

      【解决方案2】:

      看看这些配置:

      defaults 配置适用于整个容器,而 columns 有自己的默认选项。

      希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-08-28
        • 1970-01-01
        • 2010-09-24
        • 2011-02-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多