PDA

View Full Version : [2.1] A bug in Ext.applyIf?



tsouza
26 Apr 2008, 8:13 AM
Hi,

I'm a newbie in component authoring and I was playing around with it. So I created a simple TabTest component that encapsulates a configuration:



Ext.TabTest = Ext.extend(Ext.TabPanel,
{
initComponent: function()
{
Ext.applyIf(this, {
width:450,
activeTab: 0,
frame:true,
defaults:{autoHeight: true},
items:[
{contentEl:'script', title: 'Short Text'},
{contentEl:'markup', title: 'Long Text'}
]
});

Ext.TabTest.superclass.initComponent.call(this, arguments);
}
});

var tabs = new Ext.TabTest({renderTo: "tabs1", width: 800});


The problem here is that "activeTab" is not being applied, I suppose it's because it's value is zero and is probably failing an "if" test that checks if the value is really there.

Regards,
Thiago Souza

hendricd
26 Apr 2008, 8:15 AM
Yes, applyIf evaluates the presence of the member (!= undefined).

tsouza
26 Apr 2008, 6:17 PM
Than, why isn't "activeTab" being copied?

evant
26 Apr 2008, 6:23 PM
In the declaration of the TabPanel class:



activeTab : null


Since applyIf only checks for undefined, I'm assuming that activeTab already exists for the object.

26 Apr 2008, 6:38 PM
Ext.applyIf works fine:

http://tdg-i.com/img/screencasts/2008-04-26_2238.swf

26 Apr 2008, 6:45 PM
Woah, i see what the OP is talking about.

26 Apr 2008, 6:53 PM
TabPanel.js
http://tdg-i.com/img/screencasts/2008-04-26_2252.png

applyIf is working.

evant
26 Apr 2008, 7:39 PM
That being said, it would probably make sense to have applyIf also write to the object if it has that property, but the value is null. I don't know if it will cause any side effects elsewhere, if one of the devs could weigh in.



applyIf: function(o, c)
{
if(o && c)
{
for(var p in c)
{
if(typeof o[p] == "undefined" || o[p] === null)
o[p] = c[p];
}
}
return o;
}

26 Apr 2008, 7:56 PM
for now, the OP can delete the attribute before applyIf is called :)

hendricd
27 Apr 2008, 9:05 AM
@evant -- That looks a bit dangerous. 'false' and 0 would also pass that ( !o[p] ) test.

evant
27 Apr 2008, 3:30 PM
Agreed, code modified above.

tsouza
28 Apr 2008, 2:02 PM
Hi,

Thanks for the modification. The main reason on using Ext.applyIf instead of Ext.apply in component configuration is to let clients override the encapsulated configuration.

Regards,
Thiago Souza

hendricd
28 Apr 2008, 2:04 PM
I would use that revised version with caution. Ext was (and many extensions) designed under the assumption that the 'If' behavior was based on member definition (undefined) not inclusive of a null value. Big difference.

Eyes open, thats all. ;)