PDA

View Full Version : Is config really working in Ext or not?



bluehipy
19 Mar 2014, 8:47 AM
A simple test case:
Ext.define('ClassA',{
xtype:'xa',
config:{
prop:{p:'A'}
}
});




Ext.define('ClassB',{
xtype:'xb',
extend:'ClassA',
config:{
prop:{p:'B'}
}
});


var
a = new ClassA(),
b = new ClassB();

console.log(a.prop, b.prop); // Object {p: "A"} Object {p: "A"}

console.log(a.getProp(), b.getProp()); //Object {p: "A"} objectClass {p: "B"}
console.log(a.getProp(), b.getProp()); //Object {p: "A"} objectClass {p: "B"}
console.log(a.prop, b.prop); // Object {p: "A"} objectClass {p: "B"}

a = Ext.create('ClassA',{prop:1});
b = Ext.create('ClassB',{config:{prop:2}});

console.log(a.prop, b.prop); // Object {p: "A"} Object {p: "A"}

console.log(a.getProp(), b.getProp()); //Object {p: "A"} objectClass {p: "B"}
console.log(a.getProp(), b.getProp()); // Object {p: "A"} objectClass {p: "B"}
console.log(a.prop, b.prop); // Object {p: "A"} objectClass {p: "B"}

a.setProp(3);
b.setProp(4);

console.log(a.prop, b.prop); //3 4
console.log(a.getProp(), b.getProp());// 3 4
console.log(a.getProp(), b.getProp()); // 3 4
console.log(a.prop, b.prop); // 3 4

Tried with 4.2.1

My conclusion is that config is mot working by anychance as expected and there is no elegant way to override a previously defined config property.

LesJ
19 Mar 2014, 10:13 AM
This is not supported (http://www.sencha.com/forum/showthread.php?282552-override-with-config-fails)

bluehipy
19 Mar 2014, 3:54 PM
Well... this is a BIG minus for Ext.

brian428
19 Mar 2014, 7:31 PM
The default constructor in Ext.Base doesn't automatically trigger an initConfig(). So all you should need to do is define a constructor, modify this.config if necessary, and call initConfig(). https://fiddle.sencha.com/#fiddle/4cv

evant
19 Mar 2014, 9:46 PM
That's not the case. The ticket @LesJ is referring to is specifically using the override keyword.

As @brian428 mentioned, OP needs to call initConfig in the constructor.

LesJ
19 Mar 2014, 9:51 PM
That's not the case. The ticket @LesJ is referring to is specifically using the override keyword.

As @brian428 mentioned, OP needs to call initConfig in the constructor.


Thanks for the correction brian428 and evant.

bluehipy
19 Mar 2014, 11:46 PM
I figured out that the initCOnfig is not called.
How so ever there is a problem in offering the correct config.

At the construction time you have a this.config that is classify, as in it being a class rather than an object. There is the config object as n the constructor argument. My guess is that you have to mere the two to get the expected result. Also you have to reset the initialized internals to get the applyers applied.




function constructor(config){
var me = this;

//building config
var cfg = Ext.Object.merge({}, me.configMap),
configNameCache = Ext.Class.configNameCache;


Ext.Object.each(me.configMap, function(propName, propValue){
if(typeof(me[propName]) !== 'undefined'){
// force applyers call
me[configNameCache[propName].initialized] = false;
}
});

cfg = {};

for(var o in me.config){
cfg[o] = me.config[o];
}

for(var o in config){
if(typeof(config[o]) != 'undefined'){
cfg[o] = config[o];
}
}

me.initConfig(cfg);

return me.callParent(arguments);
}

brian428
20 Mar 2014, 5:23 AM
You're right that you can merge the constructor config with the class-level config (using Ext.apply() or Ext.merge()). But I'm really not sure about the rest of what you're saying. As my Fiddle shows, all you need to do is call initConfig(). I do this all the time and have never had any issues, nor have I had to do anything like what you're showing in your code.

bluehipy
20 Mar 2014, 6:49 AM
Hmm... surprisingly you are right. I was adding all that uglyness because in some context applyers were not called.... how so ever I checked now again and things are happening actually....I have to see if I don't run in any static link or other problems by just by calling initCnfig with the merge between config and this.config but it seams correct :)Thanks.

brian428
20 Mar 2014, 6:50 AM
As @brian428 mentioned, OP needs to call initConfig in the constructor.

Evant, as an aside, is there actually a reason that Sencha doesn't include a call to initConfig() in the Base constructor? Seems like that would solve all sorts of problems?

gkohen
27 Mar 2014, 5:57 AM
I think it would be nice if ExtJS could provide out of the box a DefaultBase class like this one:
https://fiddle.sencha.com/#fiddle/4cv
The concept of having a default config to your class while taking into consideration the ancestor classes parents AND getting all the getters/setters/apply/resets, seems to be a pretty reoccurring pattern.

brian428
27 Mar 2014, 6:59 AM
Not a bad idea at all, and I use something similar myself. The only tweak I would make is doing an applyIf() instead of apply(), since IMO it's a bit dangerous to just stomp on the existing config by default. But that may just be me being cautious. :)

That said, I have to think there's SOME reason why Sencha's Base class doesn't do this. I admit I don't know what reason they might have for the current behavior, but it could easily be something we're not considering (or even aware of).