2012-03-01 9 views
1

저는 Play Framework를 사용하고 있으며 AJAX를 사용하여 호출 할 스크립트에 부분보기를 반환하고자합니다. 저는 ASP.NET MVC의 세계에서 왔습니다. 아주 간단한 컨셉입니다.하지만 Play에서 그 곳을 볼 수는 없습니다.Play Framework 반환 부분보기

내가하고 싶은 것이의 예 :

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> 

Me.java

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

오류가 무엇이어야 하는가? render에 전달 된 변수는 항상 같은 이름을 가지므로'render ("anitemtemplate", item)'이어야합니다. – maartencls

답변

0

그것은해야을 be :

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가/응용 프로그램/뷰/태그에있는 경우/태그의 사용처럼 main.html, 당신이 렌더링 사용해야 년에 가정

("태그 /anitemtemplate.html ") - render의 첫 번째 인수는/app/views/디렉토리에있는 템플릿의 상대 경로입니다..

그리고 BTW,

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

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