What’s Wrong With You People?
Posted by Steve Brownlee on July 14, 2008Jul 14
Proofs of the adage, “truth is stranger than fictiion”….
Town Outlaws Stripperobics
Religious cult irate over magic cracker
And from the world of code, I found this ColdFusion gem the other day. I’m not really sure what this guy was doing.
<cffunction name="getStatuses" access="remote" returntype="query">
<cfscript>
qStatuses = QueryNew("");
QueryAddRow(qStatuses, 1);
</cfscript>
<cfquery name="qStatuses" dbtype="query">
select 'Held' label, 'HELD' data from qStatuses union all
select 'Cancelled' label, 'CANCEL' data from qStatuses union all
select 'Cancel Requested' label, 'CAN-REQ' data from qStatuses union all
select 'Released' label, 'RELEASED' data from qStatuses union all
select 'Release Requested' label, 'REL-REQ' data from qStatuses
</cfquery>
<cfreturn qStatuses>
</cffunction>
Oh wait, he did it so he could build a Javascript array string… from the query… instead of just… hard-coding it?
<cffunction name="getStatusesJS" access="remote" returntype="any">
<cftry>
<cfset qStatuses = this.getStatuses()>
<cfset js = "var _statuses = {};">
<cfoutput query="qStatuses">
<cfset js = js & "_statuses['#DATA#'] = {label:'#jsstringformat(LABEL)#',data:'#DATA#'};">
</cfoutput>
<cfreturn js>
<cfcatch type="database">
<cfrethrow>
</cfcatch>
</cftry>
</cffunction>
I suppose it is far more pedestrian and not very “OOPy” to just do…
<cfset jsArray = ArrayNew(1) />
<cfset ArrayAppend(jsArray, "var _statuses = {};") />
<cfset ArrayAppend(jsArray, "_statuses['HELD'] = {label:'Held',data:'HELD'};") />
<cfset ArrayAppend(jsArray, "_statuses['CANCEL'] = {label:'Cancelled',data:'CANCEL'};") />
<cfset ArrayAppend(jsArray, "_statuses['CAN-REQ'] = {label:'Cancel Requested',data:'CAN-REQ'};") />
<cfset ArrayAppend(jsArray, "_statuses['RELEASED'] = {label:'Released',data:'RELEASED'};") />
<cfset ArrayAppend(jsArray, "_statuses['REL-REQ'] = {label:'Release Requested',data:'REL-REQ'};") />
<cfset jsString = ArrayToList(jsArray, " ") />


Steve,
I use the toScript function in Coldfusion to convert CF variables to javascript object syntax. Simply load a struct or an array, or some combination of the two, then run it through the toScript function, voila!
No string appending needed….
Here is some info:
Creates a JavaScript or ActionScript expression that assigns the value of a ColdFusion variable to a JavaScript or ActionScript variable. This function can convert ColdFusion strings, numbers, arrays, structures, and queries to JavaScript or ActionScript syntax that defines equivalent variables and values.
Returns
A string that contains a JavaScript or ActionScript variable definition corresponding to the specified ColdFusion variable value.
Category
ToScript(cfvar, javascriptvar, outputformat, ASFormat)
And the reference in livedocs…
http://livedocs.adobe.com/coldfusion/7/htmldocs/wwhelp/wwhimpl/common/html/wwhelp.htm?context=ColdFusion_Documentation&file=00000654.htm
Hey Dan, good point. Didn’t even think of that one. Just goes more to show how ridiculously complex the developer made that logic!!