Reputation: 163
What I'm trying to do is in a grails filter show a loading spinner graphic while the user is being created in the database. It takes a while to retrieve the user credentials and I wanted to show a loading spinner. And then when the user in created in the db I want to show the original requested page.
Here's my problem: I have a grails filter called accessFilter.groovy. In the before section I check if the user is in the db. And if not I do this:
if(!user){
render(view:'/loadingCredentials')
return true
}
Then in the loadingCredentails.gsp I call the create user controller.
<body onload="callCreateUser()">
<g:javascript>
function callCreateUser(){
window.location = "admin/createuser"
}
</g:javascript>
<div>
<img alt="Loading..." src="images/puff.svg"></img>
</div>
</body>
The controller goes off and creates the user and then redirects back to the home controller(or whatever page the user originally navigated to). It correctly shows the spinner but instead it or creating the user and rendering the requested page it just creates the user again and again.
So my question is... Is there a way for this to work or should I change how I'm trying to do this completely?
Upvotes: 0
Views: 419
Reputation: 20699
I wouldn't use filter for that. If you have a specific action to process, a filter would be an overkill.
Your JS code should issue an AJAX-request and start showing the spinner, the controller action would accept the request and create a user (perhaps using a transactional service) and render the "OK" JSON back upon completion.
At the end, the onSuccess
JS callback would hide the spinner and maybe do something else.
There are tons of info on all those steps, ranging from trivial jQuery
-based solutions to REST-powered single-page app with a full-blown JS MVC framework
Upvotes: 1