Extjs:通过构造函数或 initComponent 扩展类?
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,在比
initComponent
更高的 Ext 版本中添加了通过constructor
进行重写的功能,因此特定年龄的所有代码都必须使用 initComponent。如今,如果您想在调用基类 initComponent 之后执行任何操作(构造函数对此来说为时过早),您仍然需要重写 initComponent,但是在组件之前呈现。在许多情况下(例如最常见的设置配置),这两种方式实际上并不重要,大多数人都会做最方便的事情。然而,在某些情况下它很重要。First off, the ability to override via
constructor
was added in a later version of Ext thaninitComponent
, 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.让我尝试一下 ExtJS 版本 4.0-4.2 及更高版本的更新答案。
constructor()
是对象/类before create 方法。而initComponent()
是组件before show方法。对于具有子项或复杂布局的面板,您可能需要使用 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. AndinitComponent()
is the component before show method.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.
以下是 Jay Garcia 所著《ExtJS in Action》一书中的一些相关引用:
,以及稍后,根据构造函数将配置参数应用于实例:
顺便说一句,尽管 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:
And later, and in light of constructor being where config parameters get applied to the instance:
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