Grails可以撰写回复吗?

cde*_*zaq 5 grails httpresponse composition grails-controller

LinkedIn上的人们一直在以有趣的方式使用Play来处理需要由许多不同组件组成的页面:http://engineering.linkedin.com/play/composable-and-streamable-play-apps

他们如何做到的关键组成部分是Play中的"动作"返回完整的响应,因此能够通过更高级别的动作"组合"成另一个响应.

Grails似乎并没有真正从动作中返回任何东西(或者至少没有任何特定的东西),并且当你在一个动作中时,没有一种简单的方法可以调用另一个动作.

那么,Grails可以采用这种构图方式吗?

nic*_*ato 1

我看了视频,很棒的东西。

我想不出任何方法来编写严格遵循 Grails 功能的响应(你说得很好,没有简单的方法从另一个操作调用一个操作),但是您可能会获得结合 Grails 控制器的演示文稿中显示的一些好处使用 Ajax 调用进行模板渲染(是的,这显然只是一种解决方法)。

不管怎样,我会设置一个home.gsp来定义主布局:

<html>
<head></head>
<body>
    <div><h1>Title</h1></div>
    <div id="section1"></div>
    <div id="section2"></div>
</body>
Run Code Online (Sandbox Code Playgroud)

然后添加一些 Ajax:

$(document).ready(function(){
    $.ajax({
        type: "POST",
        url: "${g.createLink(controller: 'home', action: 'section1')}",
        dataType: "html",
        success: function (data){
            $('#section1').html(data);
        }
    });
    $.ajax({
        type: "POST",
        url: "${g.createLink(controller: 'home', action: 'section2')}",
        dataType: "html",
        success: function (data){
            $('#section2').html(data);
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

HomeController看起来像这样:

...
def section1() {
    // Some code to fetch cool data...
    render template: 'section1', model: data
}
def section2() {
    // Some code to fetch cool data...
    render template: 'section2', model: data
}
...
Run Code Online (Sandbox Code Playgroud)

(我省略了模板 _section1.gsp_section2.gsp

一旦 ajax 调用返回数据,模板就会在页面中呈现。此外,各个部分是独立的,这意味着您可以编辑第 1 部分的内容和布局,而无需担心第 2 部分。

为了尝试一下,我制作了一个小型(而且足够丑陋,没有太多时间)grails 应用程序(https://github.com/nicosalvato/tochi)。

这就是说,也许你的问题更多的是理论性的(“Grails 如何处理函数式编程”类型的问题)而不是实际的问题。如果我完全没有抓住要点,请随意称我为白痴:)