当前位置: 代码迷 >> Java Web开发 >> Extjs2.0的Ext.extend定义的属性,怎么赋值和初始化,怎么获取赋值后属性的值,为什么老报this.属性为空的异常,调用属性,什么时候才要this
  详细解决方案

Extjs2.0的Ext.extend定义的属性,怎么赋值和初始化,怎么获取赋值后属性的值,为什么老报this.属性为空的异常,调用属性,什么时候才要this

热度:511   发布时间:2016-04-16 21:45:21.0
Extjs2.0的Ext.extend定义的属性,如何赋值和初始化,如何获取赋值后属性的值,为什么老报this.属性为空的错误,调用属性,什么时候才要this.

//var farmId;//被选中基地id
AddMedicineTestForm = Ext.extend(Ext.form.FormPanel, {
// 用户角色数据存储器
radioGroup : null,
farmName : null,//被选中的基地名称
farmId : null,//被选中基地id
farmStore2 :null,
farmCombo2 : null,//基地名称
farmComboId : null,
farmCertNo : null,//
    certNoStore2 : null,
certNoCombo2 : null,//备案号
certNoComboId :null,
goodsStore2 : null,
goodsCombo2 : null,//蔬菜品种
goodsName : null,//被选中的蔬菜名称
chemicalStore : null,
chemicalCombo : null,//药品名称
chemicalName : null,

// 构造方法
constructor : function(_cfg) {
if (_cfg == null) {
_cfg = {}
}

Ext.apply(this, _cfg);

farmComboId = Ext.id();
certNoComboId = Ext.id();
this.farmName='';
//基地下拉选项
farmStore2 = new Ext.data.JsonStore({
//url : 'farmManage.do?action=getAllFarmNameBycid',
    url : 'farmManage.do?action=getAllFarmName',
totalProperty : 'totalCount',
root : 'rows',
fields : ['farmId', "farmName", "farmCertNo"]
});

farmCombo2 = new Ext.form.ComboBox({
fieldLabel : '基地名称',
xtype : 'combo',
name : 'farmId',
id : farmComboId,
store : farmStore2,
triggerAction : 'all',
disabled : false,
allowBlank : false,
hiddenName : 'baseRecord',
displayField : 'farmName',
valueField : 'farmId',
listeners : {
select : this.onFarmSelect
}
});

//基地备案号
certNoStore2 = new Ext.data.JsonStore({
totalProperty : 'totalCount',
root : 'rows',
fields : ['farmId', "farmCertNo", "farmCertNo"]
});
certNoCombo2 = new Ext.form.ComboBox({
fieldLabel : '备案号',
xtype : 'combo',
id : certNoComboId,
name : 'farmId',
store : certNoStore2,
triggerAction : 'all',
disabled : false,
allowBlank : false,
hiddenName : 'baseRecord',
displayField : 'farmCertNo',
valueField : 'farmId'
});

//蔬菜名称
this.goodsStore2 = new Ext.data.JsonStore({
// url : "farmProduct.do?action=getGoodsName"
totalProperty : "totalCount",
root : "rows",
fields : ["gid", "goodsName", "farmId"]
});

goodsCombo2 = new Ext.form.ComboBox({
fieldLabel : "蔬菜名称",
xtype : "combo", // 类型为下拉列表框
// id : "supplier",
name : "gid",
store : this.goodsStore2, // 指定数据
triggerAction : "all", // 显示所有数据
mode : 'remote',
readOnly : true,
disabled : this.mydisabled, // 指定该控件是否可用(mydisabled为自定义属性)
allowBlank : this.mydisabled, // 不允许为空
hiddenName : "gid", // 下拉列表框的name
displayField : "goodsName", // 要显示的值
valueField : "gid", // 要
listeners : {
focus : this.onLoad,
select : this.onGoodsFocus,
scope : this
}
});

//检测项目
chemicalStore = new Ext.data.JsonStore({
url : "chemicalManage.do?action=getChemicalName",
totalProperty : 'totalCount',
root : 'rows',
fields : ["chemicalId", "chemicalName"]
});
chemicalCombo = new Ext.form.ComboBox({
fieldLabel : "药品名称",
xtype : "combo", // 类型为下拉列表框
name : "chemicalId",
store : this.storeChemical, // 指定数据
triggerAction : "all", // 显示所有数据
// readOnly : true, // 只读
disabled : this.mydisabled, // 指定该控件是否可用(mydisabled为自定义属性)
allowBlank : true, // 不允许为空,
hiddenName : "chemicalId", // 下拉列表框的name
displayField : "chemicalName", // 要显示的值
valueField : "chemicalId",// 要提交的值
listeners : {
select : this.onChemicalNameSelected,
scope : this
}
});


//判定结果
this.radioGroup = new Ext.form.RadioGroup({
fieldLabel : '判定结果',
items : [{
boxLabel : '合格',
inputValue : "0",
name : "resJudge",
checked : true
}, {
boxLabel : '不合格',
name : "resJudge",
inputValue : "1"
}]
});

// 将父类的构造拷贝到当前类
AddMedicineTestForm.superclass.constructor.call(this, {
layout : "form", // 当前布局为form布局
frame : true, // 填充颜色
labelWidth : 80,
baseCls : "x-plain", // 统一背景色
bodyStyle : "backgroundColor:#DFE8F6;padding:10px;", // css填充10个像素
defaults : {
xtype : "textfield",
anchor : "98%"
},
items : [
         farmCombo2,
         certNoCombo2,
         goodsCombo2,
{
fieldLabel : "输入国家/地区",
name : "country",
allowBlank : false
},
{
fieldLabel : "检测项目",
name : "proTest",
allowBlank : false
},
{
fieldLabel : "检测结果(PPM)",
name : "resTest",
allowBlank : true
},
{
fieldLabel : "判定标准(PPM)",
name : "staJudge",
allowBlank : false
},
{
fieldLabel : "标准类别",
name : "staType",
allowBlank : false
},
this.radioGroup,
{
fieldLabel : "备注",
xtype : "textarea", // 多行文本框
name : "remark", // name
allowBlank : true
}]
});
},

/**
 * 表单提交
 * 
 * @param {}
 *            _url
 */
mySubmit : function(_url) {
this.getForm().submit({
url : _url,
waitTitle : "数据传输",
waitMsg : "数据传输中,请稍等...",
success : this.onSuccess,
failure : this.onFailure,
scope : this
});
},
//基地下拉选择事件
onFarmSelect : function(_combo, _r, _index) {
this.farmName = _r.get("farmName");
this.farmCertNo = _r.get("farmCertNo");
console.log(this);
farmId = _combo.value;
alert(farmId);
alert(this.farmId+"!");
certNoStore2.proxy = new Ext.data.HttpProxy({
url : 'farmManage.do?action=getFarmCertNo&farmId='
+ farmId
});
certNoCombo2.reset();
certNoStore2.load();
},
//蔬菜数据预加载
onLoad : function() {
//alert(farmId);//为什么有些属性的调用
if(farmId==null){
Ext.Msg.alert('系统消息','请选择基地');
return false;
}
this.goodsStore2.proxy = new Ext.data.HttpProxy({
url : "farmProduct.do?action=getGoodsNameById&farmId="
+ farmId
})
this.goodsStore2.reload();
},
//蔬菜下拉选择事件
onGoodsFocus : function(_combo, _r, _index) {
this.goodsName = _r.get("goodsName");
},
//检测项目选择事件
onChemicalNameSelected : function(_combo, _r, _index) {
this.chemicalName = _r.get("chemicalName");
},
/**
 * 操作成功
 * 
 * @param {}
 *            _form
 * @param {}
 *            _action 服务端返回值
 */
onSuccess : function(_form, _action) {
this.ownerCt.onClose();
Ext.MessageBox.alert("添加成功!");
// 表格数据重新加载
panel.getStore().reload();
},

/**
 * 操作失败
 * 
 * @param {}
 *            _form
 * @param {}
 *            _action 服务端返回值
 */
onFailure : function(_form, _action) {
Ext.Msg.alert("系统消息", "添加失败!");
}
});


1、为什么调用Ext.extend里定义的属性,有的前面要:this.属性名,而有的直接:属性名(如果前面加上:this.,就报错,提示this.属性不存在),并且在方法1对属性farmId赋值后,在方法2里要再获取属性farmId的值,只能直接:属性名,如果用:this.farmId,就报错。
2、Ext.extend的语法不是很明白,请大家指教。
3、比如,上面定义了两个属性
farmName : null,//被选中的基地名称
farmId : null,//被选中基地id
1)在方法
//基地下拉选择事件:onFarmSelect :里
this.farmName = _r.get("farmName");//对farmName赋值
farmId = _combo.value;   //对farmId赋值
为什么farmName前面可以加this.
而farmId前面加this.的话,就报this.farmId为空的错误。
2)在方法
//蔬菜数据预加载:onLoad里
重新获取farmName和farmId的值,只能:alert(farmId);而不能this.farmId
------解决思路----------------------
Ext.extend这是一个类的继承问题,this.farmName = _r.get("far,mName"); 因为前面有this.farmName =“ ”,如果子类中没有该方法调用就会出错,farmId是变量就直接赋值了。Ext.extend,是子类继承父累的关系,子类一定要有这个方法才能调用。

详细参考累的继承http://blog.csdn.net/jerrysbest/article/details/6639460