2012-07-18 57 views
0

我是一名新手Grails用戶,我對AJAX完全陌生。我並不完全理解AJAX的概念,並且在線材料是分散的。Ajax和Grails for Dummies

從我的理解,在Grails中,如果我想在我的控制器中的一個執行方法,當我的HTML文檔加載的一部分,我可以簡單地使用一些沿

<div onload="${remoteFunction(action:"foo", update:"foo"...)}" ...> 
  1. 行如何來自調用假想foo的響應返回,以及如何在js函數中訪問它?
  2. 我可以從我從假設的foo動作創建的類中返回一個對象嗎?

回答

4

在foo動作的返回中,您可以將簡單的html作爲文本或渲染可以在視圖中使用的某些對象。

這裏有所有關於控制器的信息「渲染」

http://grails.org/doc/latest/ref/Controllers/render.html

你可以有一個與該數據將更新與它在那裏工作。然後,您可以像通常那樣使用JavaScript訪問該「foo」div中的Html和數據。

例如:

Controller.groovy

// renders text to response 
render '<div id="bar" onclick="alert($('bar').val())>some text</div>' 

View.gsp

//Makes the call and updates foo 
<div onload="${remoteFunction(action:"foo", update:"foo"...)}" ...> 
<div id="foo" name="foo"></div> 

輸出

<div onload="theAjaxJavascriptFunctionThatGrailsWillInject" ...> 
<div id="foo" name="foo"> 
    <div id="bar" onclick="alert($('bar').val())">some text</div> 
</div> 

我你從Controller.grooy返回一些對象,然後你有E要像對待這你View.gsp

//Makes the call and updates foo 
<div onload="${remoteFunction(action:"foo", update:"foo"...)}" ...> 
<div id="foo" name="foo"> 
    ${myObject.theValueIWant} 
</div> 

我添加了一個JavaScript警告,但你可以做到這一點,你喜歡的方式,有很多方法可以做到這一點。

希望它有幫助:)