PDA

View Full Version : Calling loadModel method inside the initComponent of a FormPanel class



jedihua
19 Oct 2010, 5:54 AM
Hi, Does anyone have any ideas on how to call a loadModel method inside the initComponent of a Ext.form.FormPanel class?

jay@moduscreate.com
19 Oct 2010, 8:09 AM
this.loadModel(<your model reference>)??


load( Ext.data.Model instance ) : Ext.form.FormPanel
(Shortcut to loadModel method) Loads matching fields from a model instance into this form
Parameters:
instance : Ext.data.Model
The model instance
Returns:
Ext.form.FormPanel
this

jedihua
19 Oct 2010, 8:17 AM
Thanks for the quick reply. I tried the this.loadModel but the safari debugger gave me a undefined method error. I check the attibutes and "this" is an "HTMLDocument" rather than an object so it doesnt have an loadModel function. I've attached the code below.

SetupPanel = Ext.extend(Ext.form.FormPanel, {
layout: {
align: 'stretch'
},
scroll: 'vertical',

initComponent: function() {
this.items = [{
xtype: 'fieldset',
title: 'Setup',
items: [{
xtype: 'select',
/* add fields here*/
store: setupStore,
listeners: {
select: function (f, r, i) {
var snameGiven = setupStore.data.items[r].data.servername;
var sadrGiven = setupStore.data.items[r].data.serveraddress;
var susnGiven = setupStore.data.items[r].data.username;

user = Ext.ModelMgr.create({
'servername': snameGiven,
'serveraddress': sadrGiven,
'username': susnGiven
}, 'ServerSetup');
// call loadModel
this.loadModel(user);


}
}
}]
},{
// rest fields and buttons
}];

CA.SetupPanel.superclass.initComponent.call(this);
}

});

jay@moduscreate.com
19 Oct 2010, 8:24 AM
is 'ServerSetup' a registered model?

jay@moduscreate.com
19 Oct 2010, 8:25 AM
Also, do your fields have "name"s identical to the keys in the model?

jedihua
19 Oct 2010, 8:29 AM
Yes, I registered the model with:

Ext.regModel('ServerSetup', {
fields: [
{name: 'serverid', type: 'int'},
{name: 'servername', type: 'string'},
{name: 'serveraddress', type: 'string'},
{name: 'username', type: 'string'}
]
});

And I have fields name matching the keys in the model. Is it because something wrong in my code so the "this" is an "HTMLDocument" but not an object?

jedihua
19 Oct 2010, 8:54 AM
I think I just found the problem. In my code, I call the loadModel method inside a listener and the listener is belong to a field, so "this" is an object of that field rather than the formPanel. So is there any way to call the formPanel object inside one of its fields?

Thanks,
Hua

jedihua
19 Oct 2010, 9:11 AM
Problem solved. Just assign "this" to a variable in the initComponent method before enter the listener.

Kask
19 Oct 2010, 10:29 PM
Instead of assigning it to a variable you can set the scope for the listeners. That will put the function in the right scope and you can use this.loadModel();



listeners: {
select: function() {},
scope: this
}

jedihua
20 Oct 2010, 5:30 AM
Thanks for the help. That worked well.