当前位置: 代码迷 >> Web前端 >> Ext4.0 里, 有关怎么定义和使用类
  详细解决方案

Ext4.0 里, 有关怎么定义和使用类

热度:32   发布时间:2012-09-04 14:19:30.0
Ext4.0 里, 有关如何定义和使用类

1.)直接或间接继承自 Ext.Component的类
例如:
Ext.define('Tyds.Datetime', {
?extend: 'Ext.panel.Panel',
?kind: null,
?textname1: null,
?textname2: null,
?initComponent: function() { ?
?????if (!this.kind) {
????alert("必须提供你需要的控件类型");
??????}
??????if (!this.textname1) {
????alert("必须提供输入框名");
??????}?
??????if (!this.textname2 && (this.kind == "fromto" ||this.kind == "selectmonth" ||this.kind == "selectji" )) {
????alert("必须提供第二个输入框名");
??????}??
??????Ext.apply(this, {
???????items: [{
?????????????xtype: 'datefield',
?????????????name : this.textname1,
?????????????fieldLabel:this.textname1
????????????},{
?????????????xtype: 'textfield',
?????????????name : this.textname2,
?????????????fieldLabel: this.textname2
??????????????}]
???????});
??????this.callParent();??????
??}
?})

调用:var tt=Ext.create( 'Tyds.Datetime', {kind:"fromto", textname1:"tyd1",??textname2:"tyd22", renderTo: Ext.getBody()});

?

2)直接或间接继承自 非Ext.Component的类

因为只有直接或间接继承自 Ext.Component的类才会在constructor里调用initComponent方法。所以这时initComponent()这个方法不会在构造时执行。所以,只能以下三种方式之一构建:

?

例如:
1. Ext.define('Tyds.Datetime', {
?constructor : function(){
??this.superclass.constructor.call(this, {kind:"kk", textname1:"t1", textname2:"2kk" })
?}
})
调用:var tt=Ext.create( 'Tyds.Datetime');

?

2. Ext.define('Tyds.Datetime', {
?constructor : function(config){
??this.superclass.constructor.call(this, config)
?}
})
调用:var tt=Ext.create( 'Tyds.Datetime', {kind:"fromto", textname1:"tyd1",??textname2:"tyd22"});

?

3.Ext.define('Tyds.Datetime', {
?kind: null,
?textname1: null,
?textname2: null,
?constructor : function(config){
????????this.kind = config.kind;
????????this.textname1 = config.textname1;
????????this.textname2 = config.textname2;
?}
})
调用:var tt=Ext.create( 'Tyds.Datetime', {kind:"fromto", textname1:"tyd1",??textname2:"tyd22");


Ext4.0?<wbr>里,?<wbr>有关如何定义和使用类!在一系列的继承中要注意的是,如果重写了构造函数,则应该在必要的时候调用超类(superclass)的构造函数,initComponent和onRender也一样。否则可能会造成一些不必要的麻烦。