Sunday, April 8, 2012

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:

Load the structure into the array




Increment the index


 

Continue until the loop is completed

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




Make sure your function has returnformat="JSON">

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

No comments:

Post a Comment