PDA

View Full Version : Is this a bug or the way it will always work?



deanna
29 May 2009, 2:57 PM
When creating a component by using xtypes in a config. If the component has other xtypes in its item list it fails. You have to fix that in the initComponent function. I think it has something to do with the items being applied to a prototype and not the object (or visa versa). Is this every going to be changed? It seems like a bug. But at least could we have the usage rules spelled out if it is going to stay. I would like a way I could create a complex object with nested xtypes and it just instantiate without me having to break it up into part config and the rest initComponent.

Animal
29 May 2009, 10:26 PM
I don't understand.

This works:



new Ext.Panel({
title: 'Test',
height: 400,
width: 600,
layout: 'fit',
renderTo: document.body,
items: {
xtype: 'form',
items: {
xtype: 'textfield',
fieldLabel: 'Xtype is textfield'
}
}
});


So does this:



Ext.ComponentMgr.create({
xtype: 'panel',
title: 'Test',
height: 400,
width: 600,
layout: 'fit',
renderTo: document.body,
items: {
xtype: 'form',
items: {
xtype: 'textfield',
fieldLabel: 'Xtype is textfield'
}
}
});


And with an existing Panel, so does this:



p.add({
xtype: 'form',
autoHeight: true,
autoWidth: true,
items: {
xtype: 'textfield',
fieldLabel: 'Xtype is textfield'
}
});
p.doLayout();

Animal
29 May 2009, 10:30 PM
Hmmm.... am I imagining it, or am I detecting the sulphurous whiff of extend-to-configure?

deanna
29 May 2009, 11:36 PM
What is extend-to-configure. And whats it doing with all that sulfur.

An example where it doesn't work. If you have xtype 'type1' , and it has an item xtype 'type2' and you set up a view port and user xtype 'type1' then you get and error saying something about invalid type in items on add.

Animal
30 May 2009, 1:28 AM
OK, I check out where the precise nature of a programming error is "it says something about".

It's debuggable.

deanna
30 May 2009, 8:48 AM
Animal, this is a known bug/feature. The solution to it as defining items in initComponent has been posted in the forums before. There it was stated something about items in config were being applied to prototype and not object. I would have to experiment to find the exact rules that cause it. But I know under some conditions it works and others it doesn't.

I'm not over extending objects/classes. I have in my current project 4 or 5 extensions for specific reusable functionality, but I use those extensions a lot. I always use the simplest class I can to base any element from, and the current project has hundreds of elements with only 4 or 5 extension.

I'm not asking for a solution to the problem because there is one and it has been posted, declare items in initComponent. I'm asking if this is going to be fixed or remain the way it is. There is also a specific reason I want to know this, I'm not just bitching randomly about some part that isn't working right. I want to be able to send back config blocks from the server to define components, and some of these top level components are 3 or 4 levels deep with nested items. I need to be able to decide (for some interfaces classes I'm working on for zendx_extjs) if I have to play the initComponent game when sending those down or not. (and that makes sending a config down a bit more involved)

Here is one of the threads it was discussed http://extjs.com/forum/showthread.php?t=33767

So the question is, is it a bug or a feature?

evant
31 May 2009, 8:45 PM
You haven't posted any code, it's difficult to understand what you're saying here without an example. As you mentioned, you can't put items in the object prototype because they will be shared across all instances. However this:



An example where it doesn't work. If you have xtype 'type1' , and it has an item xtype 'type2' and you set up a view port and user xtype 'type1' then you get and error saying something about invalid type in items on add.


Doesn't really make much sense.

deanna
1 Jun 2009, 6:33 AM
If you need I can post some code. But it seems to have been a KNOWN bug for a long long time. Others have posted code, as in that post I linked. I'm wasn't trying to report on a new bug, but I ran into it a few times with extjs 3.0 and wondered if it was going to be fixed soon (like before 3.0 launches)

evant
1 Jun 2009, 8:29 AM
The behaviour will always be like that.

Anything you put in extend gets added directly to the object prototype.

deanna
1 Jun 2009, 8:41 AM
Okay, thanks. I'll write my interface around using Ext.ComponentMgr.create then. That doesn't seem to have the same problems.

deanna
1 Jun 2009, 1:48 PM
Evant, you mentioning that it was a function of Ext.extend that had the problem got me thinking. Couldn't the problem be solved like this: (My code is in the commented sections)


extend : function(){
// inline overrides
var io = function(o){
for(var m in o){
this[m] = o[m];
}
};
var oc = Object.prototype.constructor;

return function(sb, sp, overrides){
if(Ext.isObject(sp)){
overrides = sp;
sp = sb;
sb = overrides.constructor != oc ? overrides.constructor : function(){sp.apply(this, arguments);};
}
var F = function(){},
sbp,
spp = sp.prototype;

F.prototype = spp;
sbp = sb.prototype = new F();
sbp.constructor=sb;
sb.superclass=spp;
if(spp.constructor == oc){
spp.constructor=sp;
}
//------------------------------------------------------------------------------------
// Hide items from override
var items = null;
if(overrides.items){
items = overrides.items;
unset(overrides.items);
}
//------------------------------------------------------------------------------------

sb.override = function(o){
Ext.override(sb, o);
};
sbp.superclass = sbp.supr = (function(){
return spp;
});
sbp.override = io;
Ext.override(sb, overrides);
sb.extend = function(o){Ext.extend(sb, o);};
//------------------------------------------------------------------------------------
// Reassign items
if(items){
sb.items = items;
}
//------------------------------------------------------------------------------------
return sb;
};
}(),

evant
1 Jun 2009, 5:38 PM
It could, if you wanted, but this wouldn't be added to the core. Consider:



MyClass = Ext.extend(Ext.Container, {
items: [{
title: 'Foo1'
}, new Ext.Panel({
title: 'Foo2'
})]
});


This is bad, you should really only use "primitives" in the extend declaration.

deanna
1 Jun 2009, 5:45 PM
Okay, It would have helped my learning curve a year ago if this was in the extend documentation 'extend does not support including items in the override config' or something to that effect. I think it may help others also. Items in configs are used in some places so it isn't clear where they are or are not supported.