Determining what to pass back to ExtJs from ColdFusion

ColdFusion developers who do not know PHP are at a disadvantage learning ExtJS. They can play with the examples on the Sencha examples page but when they try to run the examples from their own desktop the PHP queries to the database or flat files will not work. Why? Because you are not running the PHP server on your desktop.

What do you do? Well you turn on firebug, run the example, click on the NET panel on firebug, click on XHR, find the get request, expand it, look at the response and then try to emulate it.

Here is an example response for a tree view:

[{‘text’:’Jewelry’,’id’:’1′,”cls”:”folder”},

{‘text’:’Vintage Clothing and Accessories’,

‘id’:’2′,”cls”:”folder”},

{‘text’:’Spices’,’id’:’3′,”cls”:”folder”},

{‘text’:’Art’,’id’:’4′,”cls”:”folder”},

{‘text’:’Collectibles’,’id’:’5′,”cls”:”folder”},

{‘text’:’Books’,’id’:’6′,”cls”:”folder”},

{‘text’:’Teas’,’id’:’7′,”cls”:”folder”},

{‘text’:’Clothing’,’id’:’8′,”cls”:”folder”},

{‘text’:’Exercise’,’id’:’10’,”cls”:”folder”},

{‘text’:’Send A Little Something’,’id’:’11’,”cls”:”folder”},

{‘text’:’My New Category’,’id’:’12’,”cls”:”folder”},

{‘text’:’Another New Category’,’id’:’14’,”cls”:”folder”}]

How do you emulate this response? First you must know what the [ and the { indicate.

The [ indicates you are returning an array. The { indicates you are returning a structure. Since the [ is on the outside, you need to build a structure inside an array.

So first you execute a query. Then you create an array, loop through the query. For each loop create a structure that is appended to the array.

How?

Here is an example:

SELECT CategoryID, CategoryName

FROM CategoriesTable

WHERE CategoryID = #arguments.node#

ORDER BY CategoryID

Now set up your array

<cfset var arrUsers = ArrayNew(1)>

Set the value for your index

<cfset i = 1>

Loop through the query

<cfloop query=”variables.qCats” startrow=”1″ endrow=”#variables.qCats.recordcount#”>

Define your structure

<cfset mystruct = structnew()>

Load the structure (note that you can load the array several ways: cfset mystruct.id returns an uppercase id and value, cfset mystruct[“id”] returns a lowercase id and value. Since ExtJS expects lowercase values I use mystruct[“id”].

<cfset mystruct[“id”] = “#CategoryID#”>

<cfset mystruct[“text”] = “#CategoryName#”>

<cfset mystruct[“cls”] = “folder”>

Load the structure into the array

<cfset arrUsers[i] = mystruct>

Increment the index

<cfset i = i + 1>

Continue until the loop is completed

</cfloop>

Then all you need to do is return the array users to the calling program

<cfreturn arrUsers>

Make sure your function has returnformat=”JSON”&gt;

I hope this helps. In the future I will post full examples.