How to handle ColdFusion session time outs with Ajax calls and ExtJS
Tags: extJS, Coldfusion, Ajax
I have been working on a ColdBox application for a few months now, which has a lot of Ajax calls when a user is signed in. The one thing that I have not really handled, is how do I tell the browser that the users session has timed out.
ExtJS can make this very easy, and the code that I am showing will work in both ExtJS and ExtJS Core.
I tend to do all my Ajax calls with the ColdFusion Ajax Proxy, and create a handler for the result to be handled. The one thing I have noticed is how diverse the Ajax calls are handled in ColdFusion, in terms of handling errors and other such things. And until recently a session time out was not on my mind either, that was until I thought I had better implement one.
So lets first look at a very basic Ajax call in ColdFusion.
function testAjax() {
var remoteObject = new testAjaxProxy();
remoteObject.setCallbackHandler(testAjaxProxyResults);
remoteObject.setErrorHandler(errorHandler);
}
This is very straight forward, so I won't go into to much details. But it basically setups a callback handlers for errors and results, which you can then do what is needed to get the job done.
Usually on the ColdFusion side I would do something like the following.
<cfset var local = {} />
<cfset local.result['status'] = 'failed' />
<cfreturn local.result />
</cffunction>
Now this is a very basic representation of what I normally do, and demonstrates that by returning a struct I am giving a bit more control to the client in how to handle what is actually happening on the server as well.
So if I wanted to make sure that an exception was handled correctly, I could do something like the following in that block of code.
<cfcatch type="database">
<cfset local.result['message'] = 'Something went wrong here' />
</cfcatch>
</cftry>
From here we can then look at the result handler and do something like this in the client.
if(result.status === 'failed') {
alert(result.messge);
}
}
As basic as this is it now means that we can handle all the messages that we need to do, and provides a cleaner way of reporting information back to the client and more importantly the user.
Now the downside to this is that if an error occurs on the server that is to do with session management, we could do a simple modification and check for the desired result and do a simple relocation in the browser to either log back in, or what ever is required. But I would like to discuss events in JavaScript, and how we can use listeners to do this for us as well.
The first thing we need to do is setup our events in ExtJS, the only thing you need to make sure, is that this is obviously setup and running before you call your Ajax stuff.
myEvents.on('timedout', function() {
alert('We have timed out');
});
This is very straight forward, and it is just saying that we wish to create a listener. And to call or fire this event we just need to do the following.
Now one could argue that we really don't need to do this, and a simple relocation would be suffice. However this promotes a DRY approach and a one location to make a change if the URL was to change, and you are not replacing a lot of relocations if it does.
There are no comments for this entry.



TweetBacks