Using a CFC in AJAX is easy. Just make sure to pass your arguments in and use the arguments scope within the cfc and then use them in a cfproc param within your code. This way you can pass multiple parameters into your sql stored procedure.
The benefit of this I found is that I have greater security on the sql query and can validate the parameters being sent to SQL. I can created indexes and update the stats on the SQL Stored Proc etc which I have not found a way to do in CF yet. My code is compiled per se, and my data is returned much faster. Within my SQL Stored Proc I use SQL CTEs as much as possible so that I work on sets of data and not rows of data. For example here is a sql select that concatenates a full composite address (street number to zip code including apartments), this returns over 12000 rows into a common table expression that is returned. You can pass your parameter to the where clause of the select from CTE statement.
CTE EXAMPLE:
;with cteData as (
select id as value,
REPLACE(RTRIM(LTRIM(RTRIM(streetnumber))
+’ ‘+ LTRIM(RTRIM(REPLACE(RTRIM(ISNULL(streetprefix,”))
+’ ‘+ RTRIM(ISNULL(street,”))
+’ ‘+ RTRIM(ISNULL(streettype,”))
+’ ‘+ RTRIM(ISNULL(streetsuffix,”)),’ ‘,’ ‘)))
+’ ‘+ LTRIM(RTRIM(ISNULL(apartment,”))))+’,’+
+’ ‘+ LTRIM(RTRIM(ISNULL(city,”)))+’,’+
+’ ‘+ LTRIM(RTRIM(ISNULL(state,”)))
+’ ‘+ LTRIM(RTRIM(ISNULL(postalcode,”))),’ ‘,’ ‘)
as label from tableName
)
select value, label from cteData
AJAX EXAMPLE:
$.ajax{
type: ‘post’,
url: ‘../assets/cfc/getData.cfc’,
data: {
method: “getData”,
testing: ‘1’,
qry: ‘0’
},
dataType: ‘json’,
success: function(data){
console.log(data);
},
fail: function(data){
console.log(data);
}
});
The AJAX calls passes two parameters to the cfc (testing & qry).
CFC cffunction:
<cffunction name=”getData” displayname=”Get Data” access=”remote” returntype=”Any” returnformat=”JSON” >
<cfargument name=”testing” required=”true” type=”numeric” default=”1″ />
<cfargument name=”qry” required=”true” type=”numeric” />
<!— Define variables —>
<cfset var q=””>
<!— Get data —>
<cfstoredproc procedure=”usp_SQL_STORED_PROC_NAME” datasource=”DATASOURCE”>
<cfprocresult name=”q” />
<cfprocparam type=”in” cfsqltype=”CF_SQL_INTEGER” value=”#arguments.testing#” maxlength=”1″/>
<cfprocparam type=”in” cfsqltype=”CF_SQL_INTEGER” value=”#arguments.qry#” maxlength=”1″/>
</cfstoredproc>
<!— And return it —>
<cfset q = serializeJSON(q,”struct”)>
<cfreturn q>
</cffunction>
I serialize the the JSON results into a struct by adding a <cfset q = serializeJSON(q,”struct”)> and find that my JS / CF front end consumes my data much faster.
Running this with with testing parameter = 0 returns to whole data set whereas testing = 1 returns the top 10.
Here is a sample of the data returned:
Here are the stats for testing = 0 (all rows)
The result is 12,533 rows of serialized JSON.
Note this in on local laptop so yes it is fast but a laptop is not a server.
Here are the stats for testing = 1 (top 10 rows):
As you can see using stored procedures in CFCs called from AJAX is really simple and really fast thanks to CF.
Jim
You must be logged in to post a comment.