CFAJAX Return Types
CFAJAX 1.2 can return CF strings, numerics, booleans, arrays, structures, queries and CFCs. Each of these has a corresponding JS object.
String
The return for a CF string is a JS string. No surprises there.
Numeric
Numerics returned from CF become JS strings, as you will see if you return a simple numeric and then use the + operator in JS to try to add something to it. Convert the return in JS to a number first, using whichever method you prefer (the unary + is the quickest, e.g. var myCalc = +return + 23;)
Boolean
CFAJAX returns YES and NO (note the CAPS) for boolean true and false, which need converting for use in JS as booleans.
Array
Only single dimension arrays are supported. CF arrays start at index 1, while JS arrays start at index 0, so remember to subtract 1 from the CF array index in your JS code. Other than that it's straight forward.
Structure
A CF struct comes back as an array of objects. Each object has two properties, KEY and VALUE (note the CAPS; the key itself is also in CAPS as in the example), representing the key and value of a member in the CF struct; the array contains each of those members e.g.:
CF
<cfset mystruct = StructNew()>
<cfset mystruct.mynumber = 1>
<cfset mystruct.mystring = "hello">
<cfreturn mystruct>
JS
theStructNumberKey = return[0].KEY // returns "MYNUMBER"
theStructNumberValue = return[0].VALUE // returns "1"
theStructStringKey = return[1].KEY // returns "MYSTRING"
theStructStringValue = return[1].VALUE // returns "hello"
Query
The JS object that represents a CF query is an array of row objects, with each column as a property of each object. E.G. a cfquery that returns columns ID, NAME, DATE, SIZE and has 10 rows would become a JS array of length 10, with each element an object with propeties ID, NAME, DATE, SIZE (note the CAPS - all column names are capitalised in the JS object):
myFirstRowID = result[0].ID;
mySecondRowDate = result[1].DATE;
etc
Note that this differs from CF itself, where a query object can be referenced as a set of column arrays:
<cfset myFirstRowID = myQuery["ID"][1]>
<cfset mySecondRowDate = myQuery["DATE"][2]>
CFC
CFC properties declared in THIS (or set in that scope externally) return as JS properties with the names CAPITALISED e.g.:
CF
<cfset THIS.MyNumber = 1>
<cfset THIS.MyString = "Whatever">
<cfreturn THIS>
JS
theCFCMyNumber = return.MYNUMBER // returns "1"
theCFCMyString = return.MYSTRING // returns "whatever"
To facilitate setting up and returning a CFC externally to the CFC code, just create an CFC file called "cfobject.cfc", containing only empty
<cfset object = CreateObject("Component","cfobject")>
<cfset object.MyNumber = 1>
<cfset object.MyString = "Whatever">
The above techinque can be seen in the zip lookup examples.

There are no comments for this entry.
[Add Comment]