(Quick Reference)
remoteFunction
Purpose
Creates a remote JavaScript function that can be assigned to a DOM event to call the remote method
Examples
Example controller for an application called
shop
:
class BookController { def list() {
[books: Book.list(params)]
} def show() {
[book: Book.get(params.id)]
} def bookByName() {
[book: Book.findByName(params.bookName)]
}
}
Example usages for above controller:
$('mydiv').onclick = <g:remoteFunction action="show" id="1" />
Example as a method call in GSP only:
<select onchange="${remoteFunction(action: 'bookByName',
update: [success: 'great', failure: 'ohno'],
params: '\'bookName=\' + this.value')}">
<option>first</option>
<option>second</option>
</select>
Example changing the asynchronous option to false:
<select from="[1,2,3,4,5]" onchange="${remoteFunction(action: 'bookByName',
update: [success: 'great', failure: 'ohno'],
options: '[asynchronous: false]'}" />
Description
Attributes
action
(optional) - the name of the action to use in the link; if not specified the default action will be linked
controller
(optional) - the name of the controller to use in the link; if not specified the current controller will be linked
id
(optional) - The id to use in the link
update
(optional) - Either a Map containing the elements to update for 'success' or 'failure' states, or a string with the element id to update, in which case failure events would be ignored
before
(optional) - The JavaScript function to call before the remote function call
after
(optional) - The JavaScript function to call after the remote function call
asynchronous
(optional) - Whether to do the call asynchronously (defaults to true
, specified in 'options' Map)
method
(optional) - The method to use the execute the call (defaults to POST, specified in 'options' Map)
Events
onSuccess
(optional) - The JavaScript function to call if successful
onFailure
(optional) - The JavaScript function to call if the call fails
on_ERROR_CODE
(optional) - The JavaScript function to call to handle the specified error code (eg on404="alert('not found!')")
onUninitialized
(optional) - The JavaScript function to call if Ajax fails to initialise
onLoading
(optional) - The JavaScript function to call when the remote function loads the response
onLoaded
(optional) - The JavaScript function to call when the remote function completes loading the response
onComplete
(optional) - The JavaScript function to call when the remote function completes, including any updates
Source