PDA

View Full Version : TypeError occurs when I attempt to instantiate my formPanel



KallDrexx
30 Dec 2010, 10:42 AM
I have the following FormPanel in my javascript


EditRequestForm = Ext.extend(Ext.form.FormPanel, {
labelWidth: 75,
bodyStyle: 'padding:5px 5px 0',
width: 350,
defaults: { width: 230 },

items: [{
name: 'id',
hidden: true
}, {
fieldLabel: 'Name',
name: 'name',
allowBlank: false
}, {
fieldLabel: 'Test Plan File',
name: 'testplan'
}, {
fieldLabel: 'Scheduled Time',
name: 'scheduledtime'
}],

buttons: [{
text: 'Save'
}, {
text: 'Cancel'
}]
});

When I try to create an instance of this with the following code:


var form = new EditRequestForm({
header: false
});

The following exception occurs:

Uncaught TypeError: Object [object Object],[object Object],[object Object],[object Object] has no method 'add'

I can't see anything that is wrong. If I take out the `header: false` call, the same thing happens, so that's not it.

What am I doing wrong?

skirtle
30 Dec 2010, 3:16 PM
You can't specify items on the prototype like that. You'd need something like:


initComponent: function() {
this.items = [
...
];

EditRequestForm.superclass.initComponent.call(this);
}

In general, putting objects on the prototype is bad because they will be shared between all instances. That said, I've never really understood why Ext doesn't allow items to be specified on the prototype like this when using xtypes... it's always struck me as a potentially neat use for xtypes. From my own digging it seems that the only reason this doesn't work is because of a line in Ext.Container:initComponent() which uses delete this.items rather than this.items = null. As delete doesn't touch the prototype chain it leaves this.items as an array which confuses initItems().

If anyone knows of a good reason why Ext couldn't support prototyped items via xtypes I'd be fascinated to hear it.