ExtJS:通过Prototype上的函数设置属性是否是一种安全模式? [英] ExtJS: settings properties via a function on the Prototype: is it a safe pattern?

查看:198
本文介绍了ExtJS:通过Prototype上的函数设置属性是否是一种安全模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个围绕ExtJS3编写的大型ExtJS代码库,它具有很多以下initComponent()模式:

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

initComponent:function(){
var me = this;

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

我。 callParent();
}

现在我已经开始直接在原型上设置这个属性做这样的事情:

  Ext.define('PVE.form.BackupModeSelector',{
extends :'PVE.form.KVComboBox',
alias:['widget.pveBackupModeSelector'],
comboItems:[
['snapshot',gettext('Snapshot')],
['暂停,gettext('Suspend')],
['stop',gettext('Stop')]
],
initComponent:function doStuff(){
console.log 我们真的需要做的东西在这里+ this.comboItems);
}

});

这与ExtJS5一起使用,但它是一种安全模式吗?当我调用initComponent时,我可以确定comboItem是否已经设置?
我知道 https://docs.sencha.com /extjs/5.1/core_concepts/classes.html#Configuration config对象,但这似乎是过度的。

解决方案

在第一种方法是强制使用comboItems值。



在第二种情况下,comboItems可以在config中使用默认值。这意味着你可以覆盖它。

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

这取决于你是否希望这个值暴露出来。


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.

解决方案

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.

这篇关于ExtJS:通过Prototype上的函数设置属性是否是一种安全模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆