ExtJS: settings properties via a function on the P

2019-09-10 13:07发布

问题:

I am working on a large ExtJS codebase written around ExtJS3 which has a lot of the following initComponent() pattern:

Ext.define('PVE.form.BackupModeSelector', {
extend: 'PVE.form.KVComboBox',
alias: ['widget.pveBackupModeSelector'],

initComponent: function() {
var me = this;

me.comboItems = [
    ['snapshot', gettext('Snapshot')],
    ['suspend', gettext('Suspend')],
    ['stop', gettext('Stop')]
];

me.callParent();
}

now I have started to set this properties directly on the prototype doing things like:

Ext.define('PVE.form.BackupModeSelector', {
extend: 'PVE.form.KVComboBox',
alias: ['widget.pveBackupModeSelector'],
comboItems: [
            ['snapshot', gettext('Snapshot')],
            ['suspend', gettext('Suspend')],
            ['stop', gettext('Stop')]
],
initComponent: function doStuff() {      
     console.log('something we really need to do stuff here'  +   this.comboItems);
}

});

This works with ExtJS5, but is it a safe pattern ? can I be sure comboItems is already set when I call initComponent ? I know about the https://docs.sencha.com/extjs/5.1/core_concepts/classes.html#Configuration config Object but this seems overkill.

回答1:

In the first way, you are forcing comboItems value.

In the second, comboItems become available in config, with a default value. That means that you can override it.

var ms = Ext.create('PVE.form.BackupModeSelector', {
    comboItems: [
        // ... someting else
    ]
});

It depends if you want this value exposed to change or not.