Javascript 如何在Extjs中获取类实例?
我定义了一个类“IMS.SellMgt.testForm”。当我单击“提交”按钮时,我想得到“var test=this.formPanel;”,为什么测试是空的?如果我想得到这个。formPanel,我该怎么办?谢谢Javascript 如何在Extjs中获取类实例?,javascript,extjs,Javascript,Extjs,我定义了一个类“IMS.SellMgt.testForm”。当我单击“提交”按钮时,我想得到“var test=this.formPanel;”,为什么测试是空的?如果我想得到这个。formPanel,我该怎么办?谢谢 Ext.define('IMS.SellMgt.testForm', { formPanel: null, LoadForm: function () { this.formPanel = new Ext.form.Panel({
Ext.define('IMS.SellMgt.testForm', {
formPanel: null,
LoadForm: function () {
this.formPanel = new Ext.form.Panel({
renderTo: 'form-ct',
frame: true,
title: 'XML Form',
width: 300,
items: [{
xtype: 'fieldset',
title: 'Contact Information',
defaultType: 'textfield',
items: [{
fieldLabel: 'First Name',
emptyText: 'First Name',
name: 'first'
}
]
}],
buttons: [{
text: 'Submit',
handler: function () {
var test = this.formPanel;
}
}]
});
}
});
Ext.onReady(function () {
var frmTest = Ext.create('IMS.SellMgt.testForm', {});
frmTest.LoadForm();
});
尝试以下方法:
......
handler: function () {
var test = this.findParentByType(Ext.form.FormPanel);
}
....
handler
是为按钮指定单击事件侦听器的一种更简单的方法。因为它是简写的,所以有一些缺点
使用处理程序
,函数的上下文始终是按钮。换句话说,处理函数中的这个
是按钮,而不是表单
要使此
成为您的类,请使用具有指定范围的侦听器:
buttons: [{
text: 'Submit',
listeners: {
click: function() {
this.formPanel; // will work now
...
},
scope: this // this is the key
}
}]
您还可以使用up
查找表单:
handler: function() {
var form = this.up('form');
...
}