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

 

 

 

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

iconcombo.js



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

Ext.ux.IconCombo.js


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

Ext.ux.IconCombo.css


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

相关理论
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,而德国的那个是选中的...是吧?不过还没有图标...

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


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

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


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

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

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


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



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



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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值