PDA

View Full Version : Conditional settings in "config objects"



TheBerliner
30 Aug 2009, 6:43 PM
I am quite new to EXT and to JavaScript and I'm having a syntax problem when configuring panel or other EXT instances with what is called a "config object". I am having several such cases. In this example I am trying to make the elements of an accordion panel conditional. The panel works but I just cannot apply conditional adding of components.


var westAccord = new Ext.Panel({
id: 'westAccord',
layout:'accordion',
defaults: {blah blah}, //not important here
layoutConfig: {{blah blah}, //not important here

items: [{
id: 'accCatalogs',
title: tr.elAccCatalogs, //from translation
items: [catalogsGrid]
},
if(cfg.cfg501120) { //just pseudo-code to demonstrate
{id: 'accPageTitles', // this item should be added conditionally
title: 'blah blah',
title: tr.elAccPagesTitles,
items: [pagesGrid]
} //end config item
} //end if of pseudo-code
,{
and so forth.....Of course, this syntax does not work and it's meant here only in a symbolic form but I don't know any working syntax and I don't find any examples or the doc either.

How do you pros solve this?

I would very much appreciate a hint from the pros who they solve such problems. In my case, many components are to be configured from user settings. Unfortunately, almost all examples are using this config thing.

In my view, this "config object" (which really has nothing to do with an object) may look handy at first glance but I think it is evil, because it's not object-oriented, it is not concise in its expression and hiding vital information when one is reading it (not even the class of the instances is given). I think of it as "conf*ck".

evant
30 Aug 2009, 6:54 PM
Don't build it inline.



var items = [{
foo
},{
bar
}];
if(condition){
items.push({
baz
});
}
Ext.apply(this, {
items: items
});

TheBerliner
30 Aug 2009, 7:13 PM
Thank you very much,

the secret "push" is the decisive message for me. That solves my problem. I find it anyway cleaner to construct such settings seperately.

Proposal: Such info and examples should be added to the documentation.

Thanks again

danh2000
30 Aug 2009, 8:01 PM
...
Proposal: Such info and examples should be added to the documentation.
...


It's actually a method of the native Array object, so doesn't really live in the Ext documentation.

TheBerliner
30 Aug 2009, 8:13 PM
It's actually a method of the native Array object, so doesn't really live in the Ext documentation.
Thank you for this explanation.

But please accept my critics about your understand of a documenation. The EXT doc is frugal and does not even fulfill minimal "duties" in being complete and concise.

But a doc should be much more, it should try to explain how to use the sw (or whatever) to solve problems and it should demonstrate different approaches. In order to chive this, it must go beyond its "duties". The EXT doc is lightyears away from this, which costs very new user lots of wasted time. And the one book that exists isn't much better.

This is even more valid as there is no tool in JS like a class and methods browser where one can detect the existing protocols. I feel like flying a plane in fog and clouds without even a compass.

danh2000
30 Aug 2009, 8:17 PM
An addition of this nature, may well help users who are unfamiliar with JavaScript - feel free to make a suggestion to add into the docs.....

You must understand however that the docs are to document Ext and not the JavaScript language for which there already exists documentation and many references.

You may find this helpful:

http://www.w3schools.com/jsref/jsref_obj_array.asp
http://www.w3schools.com/JS/js_obj_array.asp

TheBerliner
30 Aug 2009, 11:46 PM
An addition of this nature, may well help users who are unfamiliar with JavaScript - feel free to make a suggestion to add into the docs.....
My impression so far was rather, that this is less welcome.

Thank you for the links. Of course, I have used this site many times.

However, how should sombody new to JS know that
{blah blah}
creates an Array?! In this mad JS world of braces and brackets?!

I knew Arrays as being declared by []. That is what was new to me here. One more example of unlogical syntax. The worst is this idiocy of where in JS to set a semicolon ";" and where not. Absolutely illogical.