View Full Version : set property dynamically

21 Aug 2009, 4:18 AM
I have this code:

// somewhere earlier
var noAccess = false;

// tbar later on
text: 'Add',
tooltip: 'Some tooltip stuff',
iconCls: 'ico_add',
disabled: function() { return noAccess; },
handler: doSomething
]Why doesn't this work? The toolbar button is disabled, although the function returns false.

If i do 'disabled: false' it works, but i want it in a function so i can use an if-else structure.

21 Aug 2009, 5:11 AM
Did you read the Api?

disabled : Boolean
True to start disabled (defaults to false)

I think your implementation shoudl be:

disabled:(function() { return noAccess; })(),

21 Aug 2009, 5:29 AM
yes, i read the API... many times already since long ;)

But thanks it works.

This worked too:

disabled:function() { return noAccess; }(),

but i don't get why it should have the '()' after the '}'.

21 Aug 2009, 5:32 AM
Well because you're specifying a *function* for something that is expecting a boolean!

if noAccess is already set, why not just specify disabled : noAccess ??

you add () to the end of a function definition to execute it.

function() { Ext.MessageBox.alert('?', 'boo!'); }();

21 Aug 2009, 5:32 AM
BTW, if you read the API, why did you specify a function instead of a boolean?!

21 Aug 2009, 5:38 AM
because i thought 'function() { ... }' would already execute it, so it would return a bool.

Well the '()' make sense now.

The bool noAccess was just an example, in my code i need some more lines with an if-else structure.