自已扩展EXT组件(Extending Ext Components)

简介: 自已扩展EXT组件 From Learn About the Ext JavaScript Library Jump to: navigation, search Summary: Gives an overview on how to extend Components ...

自已扩展EXT组件

From Learn About the Ext JavaScript Library

Jump to: navigation, search
Summary: Gives an overview on how to extend Components in Ext and provides links to the many great articles that have been written on the subject by Ext community members
Author: Patrick Donelan
Published: 2008-04-16
Ext Version: 2.0
Languages: en.pngEnglish

Contents

[hide]

img_d05563fa34a9855c9c94ef41225fc3ec.png Introduction

In the beginning there was Ext.extend() and it was good. Ext.extend() lets you build powerful object-oriented class hierarchies in javascript. You can use it to build your own base classes and derived classes, and also to extend Ext's built-in classes. If you are not familiar with Ext.extend() you should start by reading Manual:Intro:Inheritance.

Jozef Sakalos' Tutorial:Extending_Ext_Class gives a great step-by-step account of how to use the Ext 1.x approach to inheritance. It is well worth a read as the define-your-own-constructor approach still works in Ext 2.x and you will no doubt encounter pre-2.x code that uses this approach.

img_d05563fa34a9855c9c94ef41225fc3ec.png Ext.Component

Most Ext widgets (Form elements, Panels, Toolbars, ..) inherit from Ext.Component (and those that don't will in Ext 3).

img_d05563fa34a9855c9c94ef41225fc3ec.png Pre-configured Classes

The simplest way you can extend Ext.Component (or any class that inherits from it) is to do what you would have already done in Ext 1.x and use Ext.extend() (as per Jozef's tutorial above). The difference though, is that you do not need to define your own constructor (this is handled by the initComponent() function which is covered later in this article). If all you want to do is define a few of your own defaults as a re-usable class (referred to here and elsewhere in the ExtJS community as a Pre-configured Class), all you have to do is:


MyComponent = Ext.Ext.., 
    myDefault1: ,
    myDefault2: 
;
 

Ext., MyComponent;

In the example above the object literal that you pass as the second argument to Ext.extend() would contain your overwritten defaults for the config options that Ext.some.component already supports.

The above example is deceptively simple but allows you to refactor large chunks of repetitive code out of your application into re-usable objects. For example, you can then create instances of Ext.some.component with your pre-configured config options via:

 myComponent =  MyComponent;

Or lazily via the Component XType you registered, e.g. in a list of Panel items via:

..
   :  xtype:  
..

img_d05563fa34a9855c9c94ef41225fc3ec.png Extending Ext.Component

Ok, so Pre-configured Classes are useful, but so far we haven't added any extra capabilities to the classes we are inheriting from. To do that we need to override some inherited methods. This is where Ext 2.x really starts to shine.

Ext.Component existed in 1.x but in 2.x it was further expanded and improved upon, making it one of the most fundamental classes in the overall architecture. Component now provides a unified model for component creation, rendering, event handling, state management and destruction, and every component in Ext that requires these features now extends Component. Ext_2_Overview#Component_Model.

You should study the above linked Ext_2_Overview well as this document isn't much more than an elaboration on what that article lays out. You'll find that if you open up the source code for Ext.Component (it's the file called Component.js in the widgets sub-folder of the ExtJS src folder - refer to my Ext Source Overview article if you're not sure how) the code can be followed line-by-line along with the corresponding step-by-step explanation of the Component Life Cycle, Ext_2_Overview#Rendering and Ext_2_Overview#Destruction. Really eye-opening stuff if you want to start grokking how Ext works under the hood!

Now, since this article is about extending Ext.Component, the primary method you want to focus on extending is initComponent() which takes a starring role in the Component Life Cycle. After you have mastered initComponent() you can move on to other important methods such as onRender() which lets you extend Ext_2_Overview#Rendering, onDestroy() for component Ext_2_Overview#Destruction etc.. Rather than re-hashing what has been written elsewhere, at this point I'll just point you at some of the great resources that have been written by other Ext community members.

Jozef Sakalos' Tutorial:Writing_a_Big_Application_in_Ext gives a great step-by-step tutorial on using the new Component inheritance model and why it's undoubtedly a good thing for any non-trivial application.

Also by Jozef Sakalos, Tutorial:Extending_Ext2_Class also provides a great real-world example to work through (mjlecomte's forum post provides a more heavily-commented version of the same code).

img_d05563fa34a9855c9c94ef41225fc3ec.png A Re-usable Template

The following is a template (based on the template posted by Jozef Sakalos in mjlecomte's forum post) that you can use as a starting-point when extending Ext.Component classes

MyComponent = Ext.Ext.., 
    
    propA: ,
 
    initComponent: 
        
 
        
        
        
        Ext., 
            propA: 
        ;
 
        
 
        
        MyComponent..., arguments;
 
        
        
    ,
 
    
    onRender: 
 
        
 
        
        MyScope..., arguments;
 
        
 
    
;
 

Ext., MyComponent;

As an enlightening example, if you used the above class via either of the following:

 myComponent =  MyComponent
    propA: 
;

..
  : xtype: , propA: 
..

then the property propA would have been set three times, to the values 1, 2, and 3 respectively. If you follow the code (and the comments) through you will see that value starts as 1 (Constructor Default) and then is set to 2 by the user's config object, and finally is overridden in initComponent and set to 3. Hopefully this example gives you a bit of insight into the order in which the code is executed (not in the order that you read it from start to finish!).

Because components nest other components, here's a quick way to grab the top-most component.

 topCmp = oo.o=o.  o;,;

img_d05563fa34a9855c9c94ef41225fc3ec.png Closing Remarks

This article focuses primarily on the initComponent() method and its role in the Component Life Cycle. Once you get the hang of that you should start exploring the other aspects of the Ext Component model (managed rendering, state mangement, plugins, etc.) listed in Ext_2_Overview#Component_Model.

Big thanks to Jozef Sakalos who posted all the great tutorials and forum posts on this topic, and of course to Jack and the team for giving us such an awesome framework to build on. Can't wait to see what they come up with in Ext 3!

目录
相关文章
|
6月前
|
JavaScript 前端开发
vue3瀑布流布局(使用 Vue 3 框架的单文件组件格式(Single-File Component)编写的)
vue3瀑布流布局(使用 Vue 3 框架的单文件组件格式(Single-File Component)编写的)
189 0
|
7月前
|
JavaScript 前端开发 UED
单文件组件(Single-File Components):Vue.js开发的模块化之道
Vue.js是一款流行的JavaScript框架,以其简洁、灵活和易用而广受欢迎。其中一个Vue.js的强大功能就是单文件组件(Single-File Components),它使得Vue.js应用的开发更加模块化和可维护。在本博客中,我们将深入探讨单文件组件的概念、结构、用法,以及如何利用它们来构建清晰、可复用和高效的Vue.js应用。
81 0
|
JavaScript 前端开发 测试技术
ExtJS前端实现过程
从无到有的实现过程
1907 0
|
JavaScript Java 数据库
ExtJS前端工作描述
今天是17年12月31日,看来今天要在公司跨年了。经过之前两周的测试,1---成功的为自己的spring项目配置了hibernate作为持久化层的工具。也就是说,不需要使用Spring JDBC进行数据库的操作。
2316 0
|
JavaScript 前端开发 数据安全/隐私保护
|
JavaScript 前端开发
《Ext JS权威指南》——2.2节配置使用Ext JS库
本节书摘来自华章社区《Ext JS权威指南》一书中的第2章,第2.2节配置使用Ext JS库,作者:黄灯桥,更多章节内容可以访问云栖社区“华章社区”公众号查看
1357 0
|
Web App开发 前端开发 JavaScript
《Ext JS实战》——2.2 Ext.Element类
这个getElementById方法很好用,可以执行一些类似改变innerHTML的内容,或者美化和配置一个CSS类这样的基本任务。不过要是想对该节点做更多的事情,例如管理它的事件,在有鼠标点击时应用某个样式,或者替换一个CSS类?必须自己管理全部代码,还要不断地对代码进行更新,以保证能够全部浏览器的兼容性。
1705 0
EXT.JS的PROXY放在哪里,STORE放在哪里,绝对是个技术活儿啊。
我理解的是,单独的STORE,会在应用程序开始时就加载, 而VIEWMODEL的STORE,会在VIEW加载时才开始加载。 PROXY放在STORE,则会在调用这个STORE的VIEW才能请求服务器数据。
992 0