ExtJS 6:我应该使用配置对象吗?

时间:2023-01-15 09:27:30

I'm building an application with ExtJS 6. I've already read the guides, tutorials and best practice tips. But what I dont understand yet is, why should I use the config object?

我正在使用ExtJS 6构建应用程序。我已经阅读了指南,教程和最佳实践技巧。但我还不明白的是,我为什么要使用配置对象?

With config:

Ext.define('MyProject.foo.Bar', {
    extends: 'Ext.window.Window',

    ...

    config: {
        title: 'My title'
    }
});

Without config:

Ext.define('MyProject.foo.Bar', {
    extends: 'Ext.window.Window',

    ...

    title: 'My title'
});

Both are working as expected. Can anyone tell me the difference and possible benefits?

两者都按预期工作。谁能告诉我差异和可能的好处?

1 个解决方案

#1


8  

It's all described in the Class System guide:

这些都在课程系统指南中描述:

  • Configurations are completely encapsulated from other class members
  • 配置完全由其他类成员封装

  • Getter and setter methods for every config property are automatically generated into the class prototype during class creation if methods are not already defined.
  • 如果尚未定义方法,则在创建类时,每个配置属性的getter和setter方法都会自动生成到类原型中。

  • The auto-generated setter method calls the apply method (if defined on the class) internally before setting the value. You may override the apply method for a config property if you need to run custom logic before setting the value. If your apply method does not return a value, the setter will not set the value. The update method (if defined) will also be called when a different value is set. Both the apply and update methods are passed the new value and the old value as params.
  • 在设置值之前,自动生成的setter方法在内部调用apply方法(如果在类上定义)。如果需要在设置值之前运行自定义逻辑,则可以覆盖config属性的apply方法。如果您的apply方法没有返回值,则setter将不会设置该值。当设置不同的值时,也将调用更新方法(如果已定义)。 apply和update方法都将新值和旧值传递为params。

#1


8  

It's all described in the Class System guide:

这些都在课程系统指南中描述:

  • Configurations are completely encapsulated from other class members
  • 配置完全由其他类成员封装

  • Getter and setter methods for every config property are automatically generated into the class prototype during class creation if methods are not already defined.
  • 如果尚未定义方法,则在创建类时,每个配置属性的getter和setter方法都会自动生成到类原型中。

  • The auto-generated setter method calls the apply method (if defined on the class) internally before setting the value. You may override the apply method for a config property if you need to run custom logic before setting the value. If your apply method does not return a value, the setter will not set the value. The update method (if defined) will also be called when a different value is set. Both the apply and update methods are passed the new value and the old value as params.
  • 在设置值之前,自动生成的setter方法在内部调用apply方法(如果在类上定义)。如果需要在设置值之前运行自定义逻辑,则可以覆盖config属性的apply方法。如果您的apply方法没有返回值,则setter将不会设置该值。当设置不同的值时,也将调用更新方法(如果已定义)。 apply和update方法都将新值和旧值传递为params。