Ask a Jedi: Run a callback on every ColdFusion.navigate/AjaxLink

Emil asks:

I was wondering if there is a way to set a global callbackHandler for "Coldfusion.navigate" when using the "AjaxLink()" function. You see, I'm using sIFR and I wanted to put the "sIFR.replace()" inside the callbackHandler-function.

Unfortunately there is no built in way to always run a callback for ColdFusion.navigate. Nor is there anyway to do a callback at all for AjaxLink(). What I’d recommend is simply using a wrapper function to handle calling ColdFusion.navigate() and setting up a callback. For example:

<cfajaximport /> <script> function load(url,con) { document.getElementById('loadingdiv').innerHTML = "Loading content..." ColdFusion.navigate(url,con,handleResult); }

function handleResult() { document.getElementById(‘loadingdiv’).innerHTML = “” } </script>

<div id=”somediv”></div> <div id=”loadingdiv”></div>

<a href=”” onclick=”javaScript:load(‘foo.cfm?x=1’,’somediv’);return false”>x=1</a><br> <a href=”” onclick=”javaScript:load(‘foo.cfm?x=2’,’somediv’);return false”>x=2</a><br> <a href=”” onclick=”javaScript:load(‘foo.cfm?x=3’,’somediv’);return false”>x=3</a><br> </code>

This example uses a custom function, load, that simply wraps the setting of a loading message (a bit redundant since ColdFusion.navigate will show a spiner) and calling ColdFusion.navigate with the callback.

Nice and simple I think.

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