【发布时间】: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