samedi 5 juillet 2014

Using Ajax to fetch a controller value


Vote count:

0




I have a Grails view that allows users to send invitations. When the view is called I set the variable



flash.invitation = [value from database]

. This is displayed in the view:

<h3>
<g:if test="${flash.numInvites > 1 || numInvites == 0}">
You have ${flash.numInvites} invitations left
</g:if>
<g:else>
You have ${flash.numInvites} invitation left
</g:else>
</h3>


Instead of using the flash context variable I would like to use Ajax to fetch the value from the database via a controller function. For example:



def numberOfInvitesAjax() {
String inviteStr = "error: no session"
User user = getSessionUser()
if (user != null) {
int invites = user.invitations
inviteStr = Integer.toString(invites)
}
render "${inviteStr}"
}


I can't figure out how to call this function when ever the page is rendered, however (my JavaScript fu is weak).


I have tried the remoteLink tag in grails, but this fetches the value only when there is a click, but no when the page is rendered. I've tried the remoteFunction but this didn't work for me either. Also, both of these tags are marked as deprecated to be removed from Grails in the future.


Any pointers on how to use grails to fetch a value when the page is rendered?



asked 1 min ago






Aucun commentaire:

Enregistrer un commentaire