Ask a Jedi: CFAJAXPROXY and Managing Callbacks

Yaron asks:

I'd like to know what your preference is for using cfajaxproxy. In JavaScript, do you create one global proxy object and reuse it throughout your script? Or do you create a new proxy object within every function that generates a proxy call?

The reason I'm asking is I had multiple concurrent proxy calls that had their callback functions mixed up. Meaning, one functions makes 2 async calls with two separately defined callback functions. Unfortunately, one callback function received the input from another. Weird.

Ah, asynchronous network calls. Life would be a heck of a lot easier if everything was synchronous. Let’s dig a bit into what Yaron found in case it doesn’t make sense. First, consider the following CFC that we will use for our Ajax calls:

<cfcomponent output="false" extends="base">

<cffunction name=”goSlow” access=”remote” returnType=”string”> <cfargument name=”name” type=”string” required=”true”> <cfset sleep(300 * randRange(1,4))> <cfif arguments.name is “foo”> <cfset sleep(200 * randRange(1,4))> </cfif> <cfreturn “Returned from call #arguments.name#”> </cffunction>

</cfcomponent> </code>

It has one method, goSlow, that runs a randomly slow sleep call, and makes it even longer if foo is passed as an argument. It then returns the argument passed to it.

The front end code for testing will be:

<cfajaxproxy cfc="test" jsclassname="testProxy"> <script> var foo = new testProxy() var goo = new testProxy()

function handleResult(r) { console.log(r) }

foo.setCallbackHandler(handleResult) goo.setCallbackHandler(handleResult)

function runTest() { console.log(“Running test….”) foo.goSlow(‘foo’) goo.goSlow(‘goo’) console.log(‘Done with tests’) } </script>

<input type=”button” onClick=”runTest()” value=”Test”> </code>

This page makes use of cfajaxproxy to create a proxy calls called testProxy. I created two instances of it and assigned the same callback handler. The callback handler gets the result, but really has no idea who calls it. This is critical. Unless you set up some mechanism to pass in a ‘caller’ value, then you can’t tell what you are responding too. Not only can’t we tell which instance of testProxy was used, we can’t even tell what method was called.

So given that - what are some good ways to handle it? You could create a different call back handler for each instance. You can even do this inline:

foo.setCallbackHandler(handleResult) goo.setCallbackHandler(function(r) { console.log('special '+r)})

This kinda surprised me. I mean I know that this type of function (an anonymous function) isn’t jQuery only, but I didn’t start using it till I got big into jQuery. Still though, if you want to run N different methods on the proxy CFC, do you really want N different instances?

My guess is probably yes. Given that you may have one main “service” CFC to handle your Ajax calls, you could create different instances for different areas of concern. So for example:

var userProxy = new testProxy() var pageProxy = new testProxy() var cowbellProxy = new testProxy()

Each proxy in the above code sample will worry about different aspects of remote CFC service.

Can anyone else offer some advice here?

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support.

Lafayette, LA https://www.raymondcamden.com

Comments