---
title: "[翻译]Ext组件扩展概述"
author: "unifly"
date: "2008-05-28T10:36:01Z"
tags: ["EXT", "JavaScript", "框架", "配置管理"]
categories: ["Web前端"]
slug: "ext"
draft: false
lastmod: "2008-05-28T10:36:01Z"
id: 197766
url: "https://unifly.iteye.com/blog/197766"
---

心血来潮，翻译了一篇extjs上的教程，希望大家批评指正，呵呵……

原文链接：[http://extjs.com/learn/Manual:Component:Extending\_Ext\_Components](http://extjs.com/learn/Manual:Component:Extending_Ext_Components)

# Manual:Component:Extending Ext Components(Chinese)

### From Learn About the Ext JavaScript Library

Jump to: [navigation](#column-one) , [search](#searchInput)

**Summary:** 本文概述了如何扩展Ext中的组件并介绍了许多来自于Ext社区成员很好的文章。 **Author:** [Patrick Donelan](http://patspam.com/ "http://patspam.com") (译者：[unifly](../../../ "http://unifly.iteye.com/") ) **Published:** 2008-04-16 **Ext Version:** 2.0 **Languages:** ![en.png](http://extjs.com/modules/i18n/flags/en.png) [English](../../../learn/Manual:Component:Extending_Ext_Components "Manual:Component:Extending Ext Components") ![cn.png](http://extjs.com/modules/i18n/flags/cn.png) **Chinese**

## Contents

\[[hide]() ]

- [1 引言](#.E5.BC.95.E8.A8.80)
- [2 Ext.Component](#Ext.Component)
- [3 预配置类](#.E9.A2.84.E9.85.8D.E7.BD.AE.E7.B1.BB)
- [4 扩展Ext.Component](#.E6.89.A9.E5.B1.95Ext.Component)
- [5 一个可重用模板](#.E4.B8.80.E4.B8.AA.E5.8F.AF.E9.87.8D.E7.94.A8.E6.A8.A1.E6.9D.BF)
- [6 结束语](#.E7.BB.93.E6.9D.9F.E8.AF.AD)

&lt;script type="text/javascript"&gt;&lt;/script&gt;[]()

# []()[![](http://extjs.com/learn/w/skins/extjs/page_edit.png)](../../../learn/w/index.php?title=Manual%3AComponent%3AExtending_Ext_Components%28Chinese%29&amp%3Baction=edit&amp%3Bsection=1 "Edit section: 引言") 引言

起初，**Ext.extend()** 干的不错，它使你能够建立强大的面向对象的JavaScript类层次结构。你可以用它构建你自己的类和派生类，当然也可以扩展Ext的内建类。如果不熟悉Ext.extend()，你应该先读一下[Manual:Intro:Inheritance](../../../learn/Manual:Intro:Inheritance "Manual:Intro:Inheritance") 。

Jozef Sakalos写的[Tutorial:Extending\_Ext\_Class](../../../learn/Tutorial:Extending_Ext_Class "Tutorial:Extending Ext Class") 详细介绍了怎样在Ext1.X下实现继承。因为自建的构造函数在Ext2.X下仍能够工作，而且毫无疑问你将会遇到2.X之前构造方式的代码，所以本文还是很值得一读的。

[]()

# []()[![](http://extjs.com/learn/w/skins/extjs/page_edit.png)](../../../learn/w/index.php?title=Manual%3AComponent%3AExtending_Ext_Components%28Chinese%29&amp%3Baction=edit&amp%3Bsection=2 "Edit section: Ext.Component") Ext.Component

大多数Ext窗口小部件（如Form elements, Panels, Toolbars, ..）继承自Ext.Component(那些没有继承的将在[Ext3](http://extjs.com/forum/showthread.php?t=26395 "http://extjs.com/forum/showthread.php?t=26395") 中实现继承)。

[]()

# []()[![](http://extjs.com/learn/w/skins/extjs/page_edit.png)](../../../learn/w/index.php?title=Manual%3AComponent%3AExtending_Ext_Components%28Chinese%29&amp%3Baction=edit&amp%3Bsection=3 "Edit section: 预配置类") 预配置类

最简单的扩展Ext.Component的方式（或者任何继承自它的类）就是延续Ext1.X中利用**Ext.extend()** 的做法。然而还有个差别，就是你不需要定义自己的构造函数（构造函数将由initComponent()来处理，文章后面将会讲到）。如果你所需的是定义一些自己的缺省类来重用（指的是这里和ExtJS社区作为预配置类），所有你所需要做的就是：

```
//定义组件Ext.some.component的预配置类MyComponent



MyComponent = Ext.extend

(

Ext.some

.component

, {


    myDefault1: '..'

,
    myDefault2: '..'


}
)

;
 
//注册成xtype以便延迟加载



Ext.reg

(

'mycomponentxtype'

, MyComponent)

;
```

在以上示例中，Ext.extend()中的第二个参数对象包含的自定义参数可覆盖Ext.some.component中已经支持的缺省参数。

以上示例看上去较简单，但能够让你从程序中重构除去相当数量重复的代码，并生成可重用的对象。举个例子，你可以用预配置选项创建一个Ext.some.component的实例，代码如下：

```
var


 myComponent = new


 MyComponent(
)

;
```

或者通过你注册过的[Component XType](../../../learn/Ext_2_Overview#Component_XTypes "Ext 2 Overview") 延迟加载，下面示例作为Panel的组件项：

```
{

..
   items

: [

 {

xtype: 'mycomponentxtype'

}

 ]


..}

```

[]()

# []()[![](http://extjs.com/learn/w/skins/extjs/page_edit.png)](../../../learn/w/index.php?title=Manual%3AComponent%3AExtending_Ext_Components%28Chinese%29&amp%3Baction=edit&amp%3Bsection=4 "Edit section: 扩展Ext.Component") 扩展Ext.Component

好的，虽然预配置类很有用，但是目前为止我们还没有添加任何其他的功能给继承类。我们需要覆盖一些方法来实现。这是Ext2.X精彩的地方。

Ext.Component源自1.x，但是在2.x中，它得到了扩展和加强，使它成为了整个架构中最基础的类。现在组件提供统一的组件创建、渲染、事件处理、状态维护、销毁模型，而且Ext中的每个组件，凡是需要这些特性的现在都扩展自Component。[Ext\_2\_Overview#Component\_Model](../../../learn/Ext_2_Overview#Component_Model "Ext 2 Overview") 。

你应该好好的学习下上面的 [Ext\_2\_Overview](../../../learn/Ext_2_Overview "Ext 2 Overview") ，文章详细描述了组件的方方面面。当你一行行的跟踪Ext.Component中的源代码（在ExtJS的src目录下的widgets子目录下的Component.js中），你会明白[the Component Life Cycle](../../../learn/Ext_2_Overview#Component_Life_Cycle "Ext 2 Overview") , [Ext\_2\_Overview#Rendering](../../../learn/Ext_2_Overview#Rendering "Ext 2 Overview") 和[Ext\_2\_Overview#Destruction](../../../learn/Ext_2_Overview#Destruction "Ext 2 Overview") 中的步骤描述所解释的实现，将对你了解Ext如何运行很有启发。

现在，既然本文是关于扩展Ext.Component的，主要关注的方法应是**initComponent()** ，在[the Component Life Cycle](../../../learn/Ext_2_Overview#Component_Life_Cycle "Ext 2 Overview") （组件生命周期）中扮演了首要角色，在你掌握了initComponent() 后，你可以继续研究其他重要的方法，如**onRender()** ，使你能够扩展[Ext\_2\_Overview#Rendering](../../../learn/Ext_2_Overview#Rendering "Ext 2 Overview") ，**onDestroy()** 用来扩展[Ext\_2\_Overview#Destruction](../../../learn/Ext_2_Overview#Destruction "Ext 2 Overview") ，在这方面，我会指出一些由Ext社区成员写的非常好的文档。

Jozef Sakalos写的[Tutorial:Writing\_a\_Big\_Application\_in\_Ext](../../../learn/Tutorial:Writing_a_Big_Application_in_Ext "Tutorial:Writing a Big Application in Ext") 是个非常不错的教程，它教你使用新的组件继承模型，并解释了为什么组件继承模型无疑是构建优良程序的好的基础。

还有由 Jozef Sakalos写的[Tutorial:Extending\_Ext2\_Class](../../../learn/Tutorial:Extending_Ext2_Class "Tutorial:Extending Ext2 Class") ，也提供了实际的例子来讲解（mjlecomte的[帖子](http://extjs.com/forum/showthread.php?t=28085 "http://extjs.com/forum/showthread.php?t=28085") 提供了该例子的建议版本）。

[]()

# []()[![](http://extjs.com/learn/w/skins/extjs/page_edit.png)](../../../learn/w/index.php?title=Manual%3AComponent%3AExtending_Ext_Components%28Chinese%29&amp%3Baction=edit&amp%3Bsection=5 "Edit section: 一个可重用模板") 一个可重用模板

下面的模板（基于Jozef Sakalos在mjlecomte's[帖子](http://extjs.com/forum/showthread.php?t=28085 "http://extjs.com/forum/showthread.php?t=28085") 中的回复）可作为扩展Ext.Component的起点。

```
MyComponent = Ext.extend

(

Ext.some

.component

, {


    //缺省构造参数，可被自定义设置覆盖



    propA: 1

,
 
    initComponent: function


(
)
{


       //在组件初始化期间调用的代码



 
        //因为配置对象应用到了“this”，所以属性可以在这里被覆盖，或者添加新的属性



        //（如items,tools,buttons）



        Ext.apply

(

this


, {


            propA: 3


        }
)

;
 
       //调用父类代码之前        



 
        //调用父类构造函数（必须）



        MyComponent.superclass

.initComponent

.apply

(

this


, arguments)

;
 
       //调用父类代码之后



        //如：设置事件处理和渲染组件



    }

,
 
    //覆盖其他父类方法



    onRender: function


(
)
{


 
        //调用父类代码之前



 
        //调用父类相应方法（必须）



        MyScope.superclass

.onRender

.apply

(

this


, arguments)

;
 
        //调用父类代码之后



 
    }


}
)

;
 
//注册成xtype以便能够延迟加载



Ext.reg

(

'mycomponentxtype'

, MyComponent)

;
```

上面是一个启发例子，如果你用以下任意例子创建以上代码的实例 ：

```
var


 myComponent = new


 MyComponent(
{


    propA: 2


}
)

;
//或者延迟加载:



{

..
  items

: {

xtype: 'mycomponentxtype'

, propA: 2

}


..}

```

属性**propA** 将被设置3次，按顺序分别是1，2，3 。通过跟踪代码（和注释） ，你会发现初始值为1（构造缺省），然后通过用户配置对象设置为2，最后在initComponent方法中被覆盖设置成3.希望这个例子能让你稍微明白代码执行的顺序（而不是按照你读代码的顺序！）。

因为组件是其他组件的基础，所以下面代码是获取基础组件快捷方法。

```
var


 topCmp = (

function


(

o)
{

while


(

o.ownerCt

)
{

o=o.ownerCt

}

 return


 o;}

,this


)

;
```

[]()

# []()[![](http://extjs.com/learn/w/skins/extjs/page_edit.png)](../../../learn/w/index.php?title=Manual%3AComponent%3AExtending_Ext_Components%28Chinese%29&amp%3Baction=edit&amp%3Bsection=6 "Edit section: 结束语") 结束语

本文主要关注于initComponent()方法及其在[the Component Life Cycle](../../../learn/Ext_2_Overview#Component_Life_Cycle "Ext 2 Overview") 中的角色。一旦你掌握了它，就该研究[Ext\_2\_Overview#Component\_Model](../../../learn/Ext_2_Overview#Component_Model "Ext 2 Overview") 列出的Ext组件模型的其他方面（如管理渲染、状态管理、插件等）。非常感谢Jozef Sakalos，他发表了很好的关于扩展组件方面的帖子和教程，当然还要感谢Jack和Ext团队给我们提供了如此棒的框架，迫不及待想看看Ext3给我们带来了什么！