When you call a script with language="cache" and returntype="%Boolean" from a javascript script, the return value is interpreted as a string, not as a boolean.
Here's an example:
A cache script that returns (in theory) a "false" value:
<script language="cache" method="giveMeAFalse" arguments="" returntype="%Boolean" procedureblock='1'>
return 0
</script>
A javascript method that logs what the value's actually interpreted as:
function isItActuallyFalse() {
var bool = #server(..giveMeAFalse());
console.log("bool ? 'truthy':'falsy' = "+(bool ? 'truthy':'falsy'));
console.log("typeof bool = ", typeof bool);
}
</script>
Output:
bool = 0
bool ? 'truthy':'falsy' = truthy
typeof bool = string
So bool is actually the string: "0", rather than the integer 0, or the boolean false. Therefore, since bool is a string, it evaluates to truthy in Javascript, regardless of its contents. Keep this in mind when returning values from cache methods into javascript methods.
If anyone knows of official documentation of this behavior somewhere, definitely link it. I'm posting this so the next person who searches something like "cache script return type javascript csp" will hopefully hit some keywords, so it would be great if that hypothetical person also found an extended, official discussion of it.
Cheers.
See zenConvertType() [///Converts a typed value returned from the server to an appropriate JS type] from zenutils.js.
Try this:
or
This is great, thanks. Just implemented it. Worked like a charm.
Also let me just confirm (and this wasn't needed in my particular implementation): if I call giveMeAFalseMe from Cache when it's returning "true" or "false", Cache will translate "true" into a truthy %Boolean return value, right?
No, it does zenBool, which is on the client side.
The important thing is that #server()# always returns a string, for example:
PS: you can further explore the sources of the #server()# -> cspHttpServerMethod() -> cspProcessResponse(), which are in the file cspxmlhttp.js.