PDA

View Full Version : How do i get a class instance?



xiaolong85
27 Apr 2013, 6:48 PM
Hi,I defined a class 'IMS.SellMgt.testForm'.when i click the button 'submit',i want to get 'var test = this.formPanel;',why test is null? if i want to get this.formPanel,how can i do?Thank you!


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();
});

suzuki1100nz
27 Apr 2013, 8:27 PM
Scope in the button click handler is the button i.,e this - is the button.
To get a handle on formPanel you can access the parent component of the button or better use a component query looking for the form.
However I notice the formPanel property is defined outside of the buttons form.

Is 'IMS.SellMgt.testForm' meant to be an extension of form panel?

If so I think the code below is a better example of what you want to achieve.


Ext.define('IMS.SellMgt.testForm', {
extend : 'Ext.form.Panel'
alias : 'myCustomForm',

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 () {
//Get the form by xtype query
var test = this.up('form');
//Just look at the buttons parent
var test = this.ownerCt;
}
}]
});

Ext.onReady(function () {
var frmTest = Ext.create('widget.myCustomForm', {});
frmTest.show();
});