2012-07-18 61 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警告,但你可以做到这一点,你喜欢的方式,有很多方法可以做到这一点。

希望它有帮助:)