Tuesday, August 17, 2010

APEX Plugin: Javascript Attributes

Recently I was developing an APEX dynamic action plugin and ran into a bit of an issue. One of the custom attributes that I defined was a yes/no attribute which returned "true" or "false" respectively.

When I was testing my plugin it always behaved as though the user had selected yes/true even though they selected no/false. After some debugging I saw that the plugin values in Javascript were strings. Clearly strings are not booleans and they behave differently when evaluated.

To resolve this problem I just changed my if statement:

//From
if (this.action.attribute01)
//do something

//To
if (this.action.attribute01.toLowerCase() == 'true')
//do something

If you're new to JavaScript, here's how JavaScript handles strings when doing comparisons:

x = null;
console.log(x, x ? true : false);
x = '';
console.log(x, x ? true : false);
x = 'false';
console.log(x, x ? true : false);
x = 'true';
console.log(x, x ? true : false);

Here are the results:


As you can see a string returns true if it contains some data.

No comments:

Post a Comment