ExtJS: settings properties via a function on the Prototype: is it a safe pattern?

流过昼夜 提交于 2019-12-12 06:03:37

问题


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.



来源:https://stackoverflow.com/questions/32205685/extjs-settings-properties-via-a-function-on-the-prototype-is-it-a-safe-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!