【问题标题】:Play Framework Return Partial ViewPlay Framework 返回局部视图
【发布时间】:2012-03-01 15:57:03
【问题描述】:

我正在使用 Play Framework,并且使用 AJAX,希望将部分视图返回给调用脚本以进行渲染。我来自 ASP.NET MVC 世界,所以这是一个非常简单的概念,但我在 Play 中看不到它的位置。

我想做的一个例子:

Main.html

<html>
<head><title>Test</title></head>
<body>
<h1>Here's my list</h1>
<input type="text" id="new-entry" /><button id="add-new-entry">Add</button>
<ul id="item-list">
  #{list items, as:'item'}
    <li>#{anitemtemplate item}</li>
  #{/list}
</ul>

<script>
$(function() {
  $("#add-new-entry").click(function() {
    var action = #{jsAction @add(':name') /};
    var title = $("#new-entry").val();
    $.post(action(title), null, function(data) {
      var newData = $(document.createElement("li")).html(data);
      $("#item-list").append(newData);
    });
  });
});
</script>
</body>
</html>

anitemtemplate.html

${item.title} <em>by ${item.author}</em>

我的.java

public static void add(String title) {
  //add the item
  return render("anitemtemplate", newitem); //how to do this??
}

【问题讨论】:

  • 错误是什么?传递给 render 的变量总是具有相同的名称,所以它应该是 render("anitemtemplate", item)

标签: jquery playframework


【解决方案1】:

应该是:

public static void add(String title) {
  //add the item
  return render("anitemtemplate.html", item);
}

public static void add(String title) {
  //add the item
  renderArgs.put("item", newitem);
  return render("anitemtemplate.html");
}

如果 anitemtemplate.html 在 /app/views/tags/ 中,就像你的 main.html 中的标签用法一样,你应该使用 render("tags/aniitemtemplate.html") - 第一个render 的参数是模板从 /app/views/ 目录的相对路径

顺便说一句,

这个

  #{list items, as:'item'}
    <li>#{anitemtemplate item}</li>
  #{/list}

应该是

  #{list items, as:'item'}
    <li>#{anitemtemplate item /}</li>
  #{/list}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-22
    • 2012-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多