Extjs:通过构造函数或 initComponent 扩展类?

发布于 2024-09-08 06:15:34 字数 292 浏览 3 评论 0原文

在 extjs 中,您始终可以通过 constructor() 扩展 extjs 类。对于从 Component 派生的类,您还可以通过 initComponent() 进行扩展。

我想知道为什么这么多代码通过 initComponent 扩展,而 constructor 似乎是通用扩展方法。 initComponent 是否比 constructor 具有明显的优势?

In extjs you can always extend an extjs class via the constructor(). For classes derinving from Component you can also extend via initComponent().

I am wondering why so many code extend via initComponent, whereas constructor seems to be the universal extension method. Does initComponent offer clear advantage over constructor?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

家住魔仙堡 2024-09-15 06:15:34

首先,在比 initComponent 更高的 Ext 版本中添加了通过 constructor 进行重写的功能,因此特定年龄的所有代码都必须使用 initComponent。如今,如果您想在调用基类 initComponent 之后执行任何操作(构造函数对此来说为时过早),您仍然需要重写 initComponent,但是在组件之前呈现。在许多情况下(例如最常见的设置配置),这两种方式实际上并不重要,大多数人都会做最方便的事情。然而,在某些情况下它很重要。

First off, the ability to override via constructor was added in a later version of Ext than initComponent, so all code of a certain age would have to use initComponent. These days, you would still override initComponent if you want to do anything after the base class initComponent is called (constructor would be too early for this), but before the component is rendered. In many cases (like the most common, setting up configs), it does not practically matter either way and most people do whatever is most convenient. However, there are some cases where it matters.

凹づ凸ル 2024-09-15 06:15:34

让我尝试一下 ExtJS 版本 4.0-4.2 及更高版本的更新答案。

constructor() 是对象/类before create 方法。而initComponent()是组件before show方法。

constructor: function(config) {
  // ctor #1 - insert code here to modify config or run code to inject config
  // probably the cheapest place to make changes - before anything has been built

  this.callParent(arguments);

  // ctor #2 - insert code here if you need to make changes 
  // after everything has been rendered and shown, IIUC
},
initComponent: function() {
  // this function runs between ctor #1 and ctor #2

  // initComponent #1 - the UI component object tree is created,
  // (this object and child objects from config { items: [{...}]})
  // but they have not yet been rendered to DOM or shown.

  this.callParent(arguments);

  // initComponent #2 - I believe this is equivalent to ctor #2, 
  // I would prefer ctor as it is more universal.
}

对于具有子项或复杂布局的面板,您可能需要使用 initComponent,因为您需要检查和操作组件(UI 对象图)。

但对于单个表单元素(组合框、按钮等),我坚持使用构造函数,我认为它更轻(在任何复杂的对象构造或 DOM 更改之前)并且更通用。 IOW 构造函数可用于简单的 UI、模型和数据存储;后两者不能使用initComponent。

所以我只在有理由时才使用 initComponent 。通常,当我编写 initComponent 函数时,我会尝试操作子 UI 对象,下一步是将子控件提取到其自己的 Ext.define() 中,将自定义代码移动到子控件类中运行,这从父面板中删除复杂的 init。这个过程我在最新的页面中重复了四次。

Let me try an updated answer in terms of ExtJS versions 4.0-4.2 and beyond.

The constructor() is the object/class before create method. And initComponent() is the component before show method.

constructor: function(config) {
  // ctor #1 - insert code here to modify config or run code to inject config
  // probably the cheapest place to make changes - before anything has been built

  this.callParent(arguments);

  // ctor #2 - insert code here if you need to make changes 
  // after everything has been rendered and shown, IIUC
},
initComponent: function() {
  // this function runs between ctor #1 and ctor #2

  // initComponent #1 - the UI component object tree is created,
  // (this object and child objects from config { items: [{...}]})
  // but they have not yet been rendered to DOM or shown.

  this.callParent(arguments);

  // initComponent #2 - I believe this is equivalent to ctor #2, 
  // I would prefer ctor as it is more universal.
}

Panels with children or complex layout you'll probably need to use initComponent, because you'll need to inspect and manipulate the components (the UI object graph).

But for individual form elements (combobox, button, etc.) then I stick with constructor, which I believe is lighter (before any complex object construction or DOM changes) and is more universal. IOW constructors can be used for simple UI, models, and data stores; the latter two can't use initComponent.

So I only use initComponent when there's a reason to do so. Often when I write an initComponent function I'm trying to manipulate child UI objects, and my next step is to extract that child control into its own Ext.define(), the move the custom code to run in the child control class, which removes the complex init from the parent panel. This process I've repeated 4 times in my latest page.

冬天旳寂寞 2024-09-15 06:15:34

以下是 Jay Garcia 所著《ExtJS in Action》一书中的一些相关引用:

initComponent 在 Component 类的构造函数内执行,但仅在组件的一些关键设置任务发生后才执行。这些任务包括配置对象属性的缓存和应用到类的实例

,以及稍后,根据构造函数将配置参数应用于实例:

如果子类的配置实例需要通过cloneConfig 进行克隆……那么通过构造函数进行扩展是最佳选择。

顺便说一句,尽管 Jay 的书是关于 ExtJS 3 的,但克隆配置似乎在 ExtJS4 中仍然相关;请参阅:

http://docs .sencha.com/ext-js/3-4/#!/api/Ext.Component-method-cloneConfig

http://docs.sencha.com/ext-js/4-0/#!/api/Ext.Component-方法-cloneConfig

Here are some relevant quotes from Jay Garcia's book ExtJS in Action:

initComponent is executed inside the Component class’s constructor, but only after a few crucial setup tasks for the Component have taken place. These tasks include the caching and application of the configuration object properties to the instance of the class

And later, and in light of constructor being where config parameters get applied to the instance:

if configured instances of the subclass will ever need to be cloned via the cloneConfig ....then extending via the constructor is the best choice.

By the way, despite Jay's book being about ExtJS 3 it appears that cloneConfig is still relevant in ExtJS4; see:

http://docs.sencha.com/ext-js/3-4/#!/api/Ext.Component-method-cloneConfig

and

http://docs.sencha.com/ext-js/4-0/#!/api/Ext.Component-method-cloneConfig

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文