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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章