Tutorial:Extending Ext2 Class (扩展EXT2组件类)

 

目录:
1 实现的目的
2 A note for those who were used to Ext 1.x
3 文件的创建
3.1 iconcombo.html
3.2 iconcombo.js
3.3 Ext.ux.IconCombo.js
3.4 Ext.ux.IconCombo.css
4 相关理论
5 开始啰
6 完成的代码

预计实现的效果图:

实现的目的

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

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

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.

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

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

iconcombo.html

  1. <html>  
  2. <head>  
  3.     <meta http-equiv="Content-Type"  content= "text/html; charset=utf-8" >  
  4.     <link rel="stylesheet"  type= "text/css"  href= "../extjs-2.0/resources/css/ext-all.css" >  
  5.     <script type="text/javascript"  src= "../extjs-2.0/adapter/ext/ext-base.js" ></script>  
  6.     <script type="text/javascript"  src= "../extjs-2.0/ext-all-debug.js" ></script>  
  7.     <script type="text/javascript"  src= "Ext.ux.IconCombo.js" ></script>  
  8.    
  9.     <style type="text/css" >  
  10.     .ux-flag-us {  
  11.         background-image:url(../img/flags/us.png) ! important;  
  12.     }  
  13.     .ux-flag-de {  
  14.         background-image:url(../img/flags/de.png) ! important;  
  15.     }  
  16.     .ux-flag-fr {  
  17.         background-image:url(../img/flags/fr.png) ! important;  
  18.     }  
  19.     .ux-icon-combo-icon {  
  20.         background-repeat: no-repeat;  
  21.         background-position: 0 50%;  
  22.         width: 18px;  
  23.         height: 14px;  
  24.     }  
  25.     .ux-icon-combo-input {  
  26.         padding-left: 25px;  
  27.     }  
  28.     .x-form-field-wrap .ux-icon-combo-icon {  
  29.         top: 3px;  
  30.         left: 5px;  
  31.     }  
  32.     .ux-icon-combo-item {  
  33.         background-repeat: no-repeat ! important;  
  34.         background-position: 3px 50% ! important;  
  35.         padding-left: 24px ! important;  
  36.     }  
  37.     </style>  
  38.    
  39.     <script type="text/javascript" >  
  40. Ext.BLANK_IMAGE_URL = '../extjs-2.0/resources/images/default/s.gif' ;  
  41. Ext.onReady(function () {  
  42.     var  win =  new  Ext.Window({  
  43.         title:'Icon Combo Ext 2.0 Extension Class Example' ,  
  44.         width:400,  
  45.         height:300,  
  46.         layout:'form' ,  
  47.         bodyStyle:'padding:10px' ,  
  48.         labelWidth:70,  
  49.         defaults:{anchor:'100%' },  
  50.         items:[{  
  51.             xtype:'iconcombo' ,  
  52.             fieldLabel:'IconCombo' ,  
  53.             store: new  Ext.data.SimpleStore({  
  54.                     fields: ['countryCode''countryName''countryFlag' ],  
  55.                     data: [  
  56.                         ['US''United States''ux-flag-us' ],  
  57.                         ['DE''Germany''ux-flag-de' ],  
  58.                         ['FR''France''ux-flag-fr' ]  
  59.                     ]  
  60.             }),  
  61.             valueField: 'countryCode' ,  
  62.             displayField: 'countryName' ,  
  63.             iconClsField: 'countryFlag' ,  
  64.             triggerAction: 'all' ,  
  65.             mode: 'local'   
  66.         }]  
  67.     });  
  68.     win.show();  
  69. });  
  70.     </script>  
  71.     <title>Icon Combo Ext 2.0 Extension Class Example</title>  
  72. </head>  
  73. <body>  
  74. </body>  
  75. </html>  


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

iconcombo.js

  1. // vim: ts=4:sw=4:nu:fdc=2:nospell   
  2. /**  
  3.   * Ext.ux.IconCombo Extension Class for Ext 2.x Library  
  4.   *  
  5.   * @author  Ing. Jozef Sakalos  
  6.   * @version $Id: Ext.ux.IconCombo.js 617 2007-12-20 11:29:56Z jozo $  
  7.   *  
  8.   * @class Ext.ux.IconCombo  
  9.   * @extends Ext.form.ComboBox  
  10.   */   
  11. Ext.ux.IconCombo = Ext.extend(Ext.form.ComboBox, {  
  12.     initComponent:function () {  
  13.    
  14.         // call parent initComponent   
  15.         Ext.ux.IconCombo.superclass.initComponent.call(this );  
  16.    
  17.     } // end of function initComponent   
  18. });  
  19.    
  20. // register xtype   
  21. Ext.reg('iconcombo' , Ext.ux.IconCombo);  
  22.    
  23. // end of file   


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

Ext.ux.IconCombo.js

  1. // Create创建用户的扩展(User eXtensions namespace (Ext.ux))   
  2. Ext.namespace('Ext.ux' );  
  3.    
  4. /**  
  5.   * Ext.ux.IconCombo 扩展类  
  6.   *  
  7.   * @author Jozef Sakalos, aka Saki  
  8.   * @version 1.0  
  9.   *  
  10.   * @class Ext.ux.IconCombo  
  11.   * @extends Ext.form.ComboBox  
  12.   * @constructor  
  13.   * @param {Object} config 配置项参数  
  14.   */   
  15. Ext.ux.IconCombo = function (config) {  
  16.    
  17.     // 调用父类的构建函数   
  18.     Ext.ux.IconCombo.superclass.constructor.call(this , config);  
  19.    
  20. // Ext.ux.IconCombo构建器的底部   
  21.    
  22. // 进行扩展   
  23. Ext.extend(Ext.ux.IconCombo, Ext.form.ComboBox, {  
  24.    
  25. }); // 扩展完毕   
  26.    
  27. // 文件底部   

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

Ext.ux.IconCombo.css

  1. .x-flag-us {  
  2.     background-image: url(../img/flags/us.png);  
  3. }  
  4. .x-flag-de {  
  5.     background-image: url(../img/flags/de.png);  
  6. }  
  7. .x-flag-fr {  
  8.     background-image: url(../img/flags/fr.png);  
  9. }  

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

相关理论
To extend an Ext class we do not need to create a constructor function. We just need to assign the return value of Ext.extend call to a variable in our name space. Ext.extend takes the original class and a config object as arguments and returns our extension.

All tasks that were done in a custom constructor function in Ext 1.x are now done in initComponent function that we almost always override. initComponent is called early from the parent constructor function.

However, initComponent of the original class contains useful code that needs to be executed. You can see how we can call initComponent of the parent class in the above code. The pattern of calling parent functions is same for any other functions we may override.

Registering an xtype for your extension is not mandatory but it is very good idea as you can then use your extension just by typing one word of its xtype. It's also the way it is used in this tutorial.

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

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

  1. Ext.apply( this , {  
  2.             tpl:  '<tpl for=".">'   
  3.                 + '<div class="x-combo-list-item ux-icon-combo-item '   
  4.                 + '{'  +  this .iconClsField +  '}">'   
  5.                 + '{'  +  this .displayField +  '}'   
  6.                 + '</div></tpl>'   
  7.         });  

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

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

  1. .x-icon-combo-icon {  
  2.     background-repeat: no-repeat;  
  3.     background-position: 0 50%;  
  4.     width: 18px;  
  5.     height: 14px;  
  6. }  

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

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

  1. onRender: function (ct, position) {  
  2.         // call parent onRender   
  3.         Ext.ux.IconCombo.superclass.onRender.call(this , ct, position);  
  4.    
  5.         // adjust styles   
  6.         this .wrap.applyStyles({position: 'relative' });  
  7.         this .el.addClass( 'ux-icon-combo-input' );  
  8.    
  9.         // add div for icon   
  10.         this .icon = Ext.DomHelper.append( this .el.up( 'div.x-form-field-wrap' ), {  
  11.             tag: 'div' , style: 'position:absolute'   
  12.         });  
  13.     }, // end of function onRender   
  14.    
  15.     setIconCls:function () {  
  16.         var  rec =  this .store.query( this .valueField,  this .getValue()).itemAt(0);  
  17.         if (rec) {  
  18.             this .icon.className =  'ux-icon-combo-icon '  + rec.get( this .iconClsField);  
  19.         }  
  20.     }, // end of function setIconCls   
  21.    
  22.     setValue: function (value) {  
  23.         Ext.ux.IconCombo.superclass.setValue.call(this , value);  
  24.         this .setIconCls();  
  25.     } // end of function setValue   

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

  1. // 进行扩展   
  2. Ext.extend(Ext.ux.IconCombo, Ext.form.ComboBox, {  
  3.    
  4.     setIconCls: function () {  
  5.         var  rec =  this .store.query( this .valueField,  this .getValue()).itemAt(0);  
  6.         if (rec) {  
  7.             this .flag.className =  'x-icon-combo-icon '  + rec.get( this .iconClsField);  
  8.         }  
  9.     },  
  10.    
  11.     setValue: function (value) {  
  12.         Ext.ux.IconCombo.superclass.setValue.call(this , value);  
  13.         this .setIconCls();  
  14.     }  
  15.    
  16. }); // 扩展完毕   

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

  1. .x-icon-combo-input {  
  2.     padding-left: 26px;  
  3. }  
  4. .x-form-field-wrap .x-icon-combo-icon {  
  5.     top: 3px;  
  6.     left: 6px;  
  7. }  

完成的代码
这里是 IconCombo扩展的完整的代码,以备你参考用:

  1. // vim: ts=4:sw=4:nu:fdc=2:nospell   
  2. /**  
  3.   * Ext.ux.IconCombo Extension Class for Ext 2.x Library  
  4.   *  
  5.   * @author  Ing. Jozef Sakalos  
  6.   * @version $Id: Ext.ux.IconCombo.js 617 2007-12-20 11:29:56Z jozo $  
  7.   *  
  8.   * @class Ext.ux.IconCombo  
  9.   * @extends Ext.form.ComboBox  
  10.   */   
  11. Ext.ux.IconCombo = Ext.extend(Ext.form.ComboBox, {  
  12.     initComponent:function () {  
  13.    
  14.         Ext.apply(this , {  
  15.             tpl:  '<tpl for=".">'   
  16.                 + '<div class="x-combo-list-item ux-icon-combo-item '   
  17.                 + '{'  +  this .iconClsField +  '}">'   
  18.                 + '{'  +  this .displayField +  '}'   
  19.                 + '</div></tpl>'   
  20.         });  
  21.    
  22.         // call parent initComponent   
  23.         Ext.ux.IconCombo.superclass.initComponent.call(this );  
  24.    
  25.     }, // end of function initComponent   
  26.    
  27.     onRender:function (ct, position) {  
  28.         // call parent onRender   
  29.         Ext.ux.IconCombo.superclass.onRender.call(this , ct, position);  
  30.    
  31.         // adjust styles   
  32.         this .wrap.applyStyles({position: 'relative' });  
  33.         this .el.addClass( 'ux-icon-combo-input' );  
  34.    
  35.         // add div for icon   
  36.         this .icon = Ext.DomHelper.append( this .el.up( 'div.x-form-field-wrap' ), {  
  37.             tag: 'div' , style: 'position:absolute'   
  38.         });  
  39.     }, // end of function onRender   
  40.    
  41.     setIconCls:function () {  
  42.         var  rec =  this .store.query( this .valueField,  this .getValue()).itemAt(0);  
  43.         if (rec) {  
  44.             this .icon.className =  'ux-icon-combo-icon '  + rec.get( this .iconClsField);  
  45.         }  
  46.     }, // end of function setIconCls   
  47.    
  48.     setValue: function (value) {  
  49.         Ext.ux.IconCombo.superclass.setValue.call(this , value);  
  50.         this .setIconCls();  
  51.     } // end of function setValue   
  52. });  
  53.    
  54. // register xtype   
  55. Ext.reg('iconcombo' , Ext.ux.IconCombo);  
  56.    
  57. // end of file   

引用自: http://extjs.com/learn/Tutorial:Extending_Ext2_Class_%28Chinese%29

Author: Jozef Sakalos
译者:Frank Chueng

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值