2.19.07 CFAJAX + JSON == Better Javascript Object Support

I’ve been developing around CFAJAX automatically instantiating any coldfusion struct in Javascript as an array of KEY:VALUE pairs. I fell victim to several traps in CFAJAX when there were ever a KEY:VALUE pair with the VALUE being an empty string.

The more I dug around the cfajax code the more I figured it would be best to take the coldfusion structure and convert it to JSON instead of trying to rebuild it into this odd array of KEY:VALUE pairs.

Just a proof of concept - Fetch the nice “CFJSON” CFC code from http://www.epiphantastic.com/cfjson/ (authored by Jehiah Czebotar). I use the following code in the Structure section of the parseResult cffunction in cfajax.cfm:

...
<cffunction name="parseResult" returntype="string" access="private">
    ...
    <cfelseif IsStruct(arguments.var)>
        <!--- Structure --->
        <cfinvoke component="JSON" method="encode" data="#arguments.var#" returnvariable="_json_var" />
        <cfset variables.result =  variables.result & "_#id# = " & _json_var & ";"& lineFeed>
    <cfelse>
    ...
</cffunction>
...

Download this code: cfajax_json


This outputs :

****/
var _6497_1171934480366 = null;
_6497_1171934480366 = {"FIRST_NAME":"ASD",LAST_NAME":"ASD","HEIGHT":72,"_BILLING"
:{"CITY":"Mammoth Lakes","STATE":"CA "}};
DWREngine._handleResponse('6497_1171934480366', _6497_1171934480366, true, true, true);
/* EOF CFAJAX */

.. instead of :
****/
var _6497_1171934480366 = null;
_8386_1171935413063 = [
{ KEY:'FIRST_NAME',VALUE:'ASD' },
{ KEY:'LAST_NAME',VALUE:'ASD' },
{ KEY:'HEIGHT',VALUE:'72' },
{ KEY:'_BILLING',VALUE:_8386_1171935413063 = [
{ KEY:'CITY',VALUE:'Mammoth Lakes' },
{ KEY:'STATE',VALUE:'CA ' }
]
}
]
DWREngine._handleResponse(’6497_1171934480366′, _6497_1171934480366, true, true, true);
/* EOF CFAJAX */


No comments




0.353s