扩展Ext2类 Extending Ext2 Class

简介: Tutorial:Extending Ext2 Class (Chinese) From Learn About the Ext JavaScript Library Jump to: navigation, search Summary: 本文为您介绍扩展EXT组件类所需的几个步骤。

Tutorial:Extending Ext2 Class (Chinese)

From Learn About the Ext JavaScript Library

Jump to: navigation, search
Summary: 本文为您介绍扩展EXT组件类所需的几个步骤。
Author: Jozef Sakalos(译者:Frank Chueng)
Published: January 2, 2008
Ext Version: 2.0+
Languages: en.pngEnglishkr.pngKoreancn.pngChinese

Contents

[hide]

img_d05563fa34a9855c9c94ef41225fc3ec.png 实现的目的

预期将是这样的IconCombo
预期将是这样的IconCombo

要创建的扩展是一个在文字前面能够显示图标的这么一个Ext.form.Combobox。将其中一个功能举例来说,就是要在一块选择里,国家名称连同国旗一并出现。

我们先给扩展起个名字,就叫Ext.ux.IconCombo

img_d05563fa34a9855c9c94ef41225fc3ec.png A note for those who were used to Ext 1.x

Extending Ext classes has not been difficult in Ext 1.x but it is even easier in Ext 2.x and the whole matter has not dramatically changed. You can even use the same procedure in Ext 2.x as you have used in Ext 1.x. However, every line of code you don't need to type contributes to code maintainability, readability and reduces number of possible bugs. Therefore, I'll show the easiest, simplest and shortest method here.

img_d05563fa34a9855c9c94ef41225fc3ec.png 文件的创建

首要的步骤是准备好开发中将会使用的文件。需下列文件:

  • iconcombo.html: 新扩展将会使用的 html markup
  • iconcombo.js: 程序javascript代码
  • Ext.ux.IconCombo.js: 扩展的javascript文件
  • Ext.ux.IconCombo.css: 扩展样式表

img_d05563fa34a9855c9c94ef41225fc3ec.png iconcombo.html



<html>
<head>
    <meta>
    <link>
    <link>
    <script></script>
    <script></script>
    <script></script>
    <script></script>
    
    <script>Ext.onReady(iconcombo.init, iconcombo);</script>
    <title>Ext.ux.IconCombo Tutorial</title>
</head>
<body>
<div>
    <div>Icon combo:</div>
    <div></div>
</div>
</body>
</html>

该文件来自教程Ext程序规划入门 的轻微修改。

img_d05563fa34a9855c9c94ef41225fc3ec.png iconcombo.js


 

Ext. = ;
 

iconcombo =  
 
    
     
        
 
        
        init:  
             icnCombo =  Ext..
                store:  Ext..
                    fields: , , ,
                    data: 
                        , , ,
                        , , ,
                        , , 
                    
                ,
                valueField: ,
                displayField: ,
                iconClsField: ,
                triggerAction: ,
                mode: ,
                width: 
            ;
            icnCombo.;
            icnCombo.;
        
    ;
; 
 


我们在这个文件中创建IconCombo,以便可以进行扩展和测试。

img_d05563fa34a9855c9c94ef41225fc3ec.png Ext.ux.IconCombo.js


Ext.;
 

Ext.. = config 
 
    
    Ext....., config;
 
 
 

Ext.Ext.., Ext.., 
 
; 
 

运行到这一步,实际这是一个没有对Ext.form.ComboBox新加任何东西的空扩展。我们正是需要这个完成好的空扩展,再继续下一步。

img_d05563fa34a9855c9c94ef41225fc3ec.png Ext.ux.IconCombo.css

 
    background-image: ;

 
    background-image: ;

 
    background-image: ;

路径可能根据你所在的国旗放置目录有所不同。国旗的资源可在here下载。

img_d05563fa34a9855c9c94ef41225fc3ec.png Let's go

So far so good!如果你浏览iconcombo.html应该会发现一个包含三个选项的标准combo,而德国的那个是选中的...是吧?不过还没有图标...

现在正是开始工作。在调用父类构建器之后加入下列行:

. = config. ||
      
    + 
    + 
    +  + . + 
    +  + . + 
    + 
    + 
;

在这一步,我们将默认combox box的模版重写为iconClsField模版。

现在加入Ext.ux.IconCombo.css中的样式文件:

 
    background-repeat: ;
    background-position:  ;
    width: ;
    height: ;

不错!可以测试一下了,刷新的页面,还好吧!?嗯,列表展开时那些漂亮的图标就出来了。。还有。。我们不是要在关闭时也出现图标的吗?

在构建器中加入创建模版的过程:

.
    render:scope:, fn: 
         wrap = ..;
        ..position:;
        ..;
        . = Ext..wrap, 
            tag: , style:
        ;
    
;

加入 事件render的侦听器,用于调整元素样式和创建国旗的div容器。如后按照下列方式进行扩展:


Ext.Ext.., Ext.., 
 
    setIconCls:  
         rec = ..., ..;
        rec 
            .. =  + rec..;
        
    ,
 
    setValue: value 
        Ext....., value;
        .;
    
 
; 

新增 setIconCls函数并重写setValue函数。我们还是需要父类的setValue的方法来调用一下,接着再调用setIconCls的函数。最后,我们应该在文件Ext.ux.IconCombo.css加入下列代码:

 
    padding-left: ;

  
    top: ;
    left: ;

img_d05563fa34a9855c9c94ef41225fc3ec.png 完成

最后再刷新一下,如果一切顺利,那这个就是新的Ext.ux.IconCombo扩展! 希望你能在此基础上扩展更多的组件!

谢谢Brian Moeskau提醒,使得能进一步精简Ext.ux.IconCombo 代码,才称得上最终版本。最终代码和CSS为:

img_d05563fa34a9855c9c94ef41225fc3ec.png Ext.ux.IconCombo.js


Ext.;
 

Ext.. = config 
 
    
    Ext....., config;
 
    . = config. ||
           
        + . 
        +  
        + . 
        + 
    ;
 
    .
        render:scope:, fn: 
             wrap = ..;
            ..position:;
            ..;
            . = Ext..wrap, 
                tag: , style:
            ;
        
    ;
 
 

Ext.Ext.., Ext.., 
 
    setIconCls:  
         rec = ..., ..;
        rec 
            .. =  + rec..;
        
    ,
 
    setValue: value 
        Ext....., value;
        .;
    
 
; 
 

img_d05563fa34a9855c9c94ef41225fc3ec.png Ext.ux.IconCombo.css


 
    background-image../img/flags/us;

 
    background-image../img/flags/de;

 
    background-image../img/flags/fr;

 

 
    background-repeat: ;
    background-position:  ;
    width: ;
    height: ;

 
    padding-left: ;

  
    top: ;
    left: ;

 
    background-repeat: ;
    background-position:  ;
    padding-left: ;
目录
相关文章
|
5月前
Ext简单示例
Ext简单示例
|
Web App开发 前端开发 JavaScript
《Ext JS实战》——2.2 Ext.Element类
这个getElementById方法很好用,可以执行一些类似改变innerHTML的内容,或者美化和配置一个CSS类这样的基本任务。不过要是想对该节点做更多的事情,例如管理它的事件,在有鼠标点击时应用某个样式,或者替换一个CSS类?必须自己管理全部代码,还要不断地对代码进行更新,以保证能够全部浏览器的兼容性。
1705 0
|
JavaScript 开发者 API
Ext 4 概述(二)之Ext Core
Ext Core 新型类系统 参考资源 Ext JS 4的倒数:动态加载和新的类机制(上) Ext JS 4的倒数:动态加载和新的类机制(下) Ext JS 4 的类机制主要围绕传统OO模型而设计,弥补了Prototype OO不足。
676 0
|
JavaScript
Ext中的get、getDom、getCmp、getBody、getDoc的区别
Ext中包含了几个以get开头的方法,这些方法可以用来得到文档中DOM、得到当前文档中的组件、得到Ext元素等,在使用中要注意区别使用。 1、get方法 get方法用来得到一个Ext元素,也就是类型为Ext.Element的对象,Ext.Element类是Ext对DOM的封装,代表DOM的元素,可以为每一个DOM创建一个对应的Element对象,可以通过Element对象上的方法来实现
997 0