Last updated by admin 3 years ago
Ajax???? {excerpt:hidden=true}Ajax Support {excerpt}
????????? {excerpt:hidden=true}Introduction {excerpt}
{excerpt:hidden=true} Ajax stands for Asynchronous Javascript and XML and is the driving force behind the shift to richer web applications. These types of applications in general are better suited to agile, dynamic frameworks written in languages like Ruby and Groovy. Grails provides support for building Ajax applications through its Ajax tab library for a full list of these see the Tag Library Reference. {excerpt} Ajax??Asynchronous Javascript and XML???????????????????????????????????????????????????????????????Ruby? Groovy?????????????????????????framework????????Grails??Ajax???????????? Ajax????????????????????Ajax??????????????????????????????????????{excerpt:hidden=true} Rails developers will be familiar with the tag library as there are equivalent helper methods in Rails for most of Grails' Ajax tags. {excerpt} Rails??????????Grails?Ajax???Rails???????????????????????????????????????????Ajax?? {excerpt:hidden=true}Adaptive Ajax Tags {excerpt}
{excerpt:hidden=true} One major difference with Grails' tags is that they are not tied to the Prototype library. For the Grails Ajax tags to work you need to include a supported Ajax library within the "head" tag of your HTML document. The tags will then "adapt" to the included library. {excerpt} Grails????Rails????????????Prototype????????????????Grails?Ajax???????????HTML???????"head"??????????Ajax??????????????????{excerpt:hidden=true} Ajax with Prototype: {excerpt} Prototype?Ajax?:<g:javascript library="prototype" /><g:javascript library="yahoo" /><g:javascript library="dojo" />Dojo??????? {excerpt:hidden=true}Installing Dojo {excerpt}
{excerpt:hidden=true} If you choose to use dojo because it is quite a large framework it does not come bundled with Grails and requires installing. Luckily, Grails provides a target to do this for you. Type the following from the root of a Grails project and dojo will be downloaded and setup automatically: {excerpt} ??dojo??????????????????????????????Grails?????????????????????????? ????Grails?dojo????????????ant?target???????Grails?????????????????????? ???????dojo???????????????:grails install-dojo
????????????? {excerpt:hidden=true}Loading Remote Content {excerpt}
{excerpt:hidden=true} Remote content can be loaded in a number of ways, the most commons way is through the "remoteLink" tag. This tag allows the creation of HTML anchor tags that perform an asynchronous request and optionally set the response in an element. The simplest way to create a remote link is as follows: {excerpt} ???????????????????????????????????????"remoteLink"?????????????????? ???????????????????????????????HTML????????????????????????????????????? ???????:<g:remoteLink action="delete" id="1">Delete Book</g:remoteLink>
def delete = {
def b = Book.get( params.id )
b.delete()
render "Book ${b.id} was deleted"
}<div id="message"></div> <g:remoteLink action="delete" id="1" update="message">Delete Book </g:remoteLink>
<div id="message"></div> <div id="error"></div> <g:remoteLink action="delete" id="1" update="[success:'message',failure:'error']">Delete Book </g:remoteLink>
??????????? {excerpt:hidden=true}Handling Events {excerpt}
{excerpt:hidden=true} Specific javascript can be called if certain events occur, all the events start with the "on" prefix and allow you to give feedback to the user where appropriate, or take other action: {excerpt} ?????????????????javascript?????????????????????????????"on"?????????????????????????????????????????????????????????:<g:remoteLink action="show" id="1" update="sucess" onLoading="showProgress();">Show Book 1 </g:remoteLink>
- onSuccess (optional) - The javascript function to call if successful
- onFailure (optional) - The javascript function to call if the call failed
- on_ERROR_CODE (optional) - The javascript function to call to handle specified error codes (eg on404="alert('not found!')")
- onUninitialized (optional) - The javascript function to call the a ajax engine failed to initialise
- onLoading (optional) - The javascript function to call when the remote function is loading the response
- onLoaded (optional) - The javascript function to call when the remote function is completed loading the response
- onComplete (optional) - The javascript function to call when the remote function is complete, including any updates{excerpt}
- onSuccess (?????) - ????????????javascript??
- onFailure (?????) - ????????????javascript??
- on_ERROR_CODE (?????) - ?????????????????????javascript?? (??? on404="alert('not found!')")
- onUninitialized (?????) - ajax?????????????????????javascript??
- onLoading (?????) - ????????????????????????javascript??
- onLoaded (?????) - ???????????????????????????javascript??
- onComplete (?????) - ?????????????????????????????javascript??
???????????????? {excerpt:hidden=true}Submitting a Form remotely {excerpt}
{excerpt:hidden=true} An HTML form can also be submitted asynchronously in one of two ways. Firstly using the "formRemote" tag which expects similar attributes to those for the "remoteLink" tag: {excerpt} ???HTML??????????????????2?????????????"formRemote"?????????"remoteLink"?????????????????:<g:formRemote url="[controller:'book',action:'delete']" update="[success:'message',failure:'error']"> <input type="hidden" name="id" value="1" /> <input type="submit" value="Delete Book!" /> </g:formRemote >
<form action="delete"> <input type="hidden" name="id" value="1" /> <g:submitToRemote action="delete" update="[success:'message',failure:'error']" /> </form>
Ajax?Render???? {excerpt:hidden=true}Ajax and the Render Method {excerpt}
{excerpt:hidden=true} Grails' render method is the perfect companion for creating Ajax responses in a number of ways. As an example lets take a look at an example that retrieves the current date and time: {excerpt} Grails?render???????????Ajax?????????????????????????????????????????????:…
// controller action
def time = {
render "${new Date()}"
}
…
<g:remoteLink action="time" update="time">Get Time</g:remoteLink>
<div id="time">Time to be displayed here</div>…
// controller action
def time = {
render(contentType:'text/xml') {
time(new Date())
}
}
…
// resulting response
<time>(the current time)</time>…
// controller action
def time = {
render(builder:'json') {
time(new Date())
}
}
…
// resulting JSON response
{ time: "..." }…
// controller action
def time = {
render(builder:'rico') {
element(id:'time') {
new Date()
}
}
}
…
// resulting Rico response
<ajax-response>
<response type="element" id="time">
… // current time here
</response>
</ajax-response>


