首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Extjs:通过构造函数或initComponent扩展类?

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

Stack Overflow用户
提问于 2010-07-03 23:38:05
回答 3查看 20.6K关注 0票数 23

在extjs中,您总是可以通过constructor()扩展extjs类。对于从Component中分离出来的类,您也可以通过initComponent()进行扩展。

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-07-04 01:35:32

首先,在比initComponent更晚的Ext版本中添加了通过constructor覆盖的功能,因此特定年龄的所有代码都必须使用initComponent。现在,如果你想在调用基类之后重写initComponent (构造函数对此来说还为时过早),但在呈现组件之前先,那么你仍然可以重写initComponent。在许多情况下(就像最常见的设置配置一样),无论哪种方式实际上都无关紧要,大多数人都会做最方便的事情。然而,在某些情况下,它很重要。

票数 18
EN

Stack Overflow用户

发布于 2013-07-12 22:19:37

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

constructor()是create方法之前的对象/类。initComponent()是显示方法之前的组件。

代码语言:javascript
复制
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()中,移动自定义代码以便在子控件类中运行,这将从父面板中删除复杂的初始化。这个过程我已经在我的最新页面中重复了4次。

票数 12
EN

Stack Overflow用户

发布于 2012-01-04 06:07:55

以下是杰伊·加西亚的书《ExtJS in Action》中的一些相关语录:

initComponent在组件类的构造函数内执行,但只有在组件的几个关键设置任务完成之后才会执行。这些任务包括缓存配置对象属性并将其应用于类的实例

然后,根据构造函数将配置参数应用到实例的位置:

如果需要通过cloneConfig克隆子类的已配置实例,则通过构造函数扩展....then是最佳选择。

顺便说一句,尽管Jay的书是关于ExtJS 3的,但cloneConfig似乎仍然与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-method-cloneConfig

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3171946

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档