Goal is to go from a Model/ViewModel written in Scala to raw JSON that can be bootstrapped into the view template so as to avoid making requests for the JSON data after the page load.
目标是从用Scala编写的Model / ViewModel转换为可以引导到视图模板中的原始JSON,以避免在页面加载后发出对JSON数据的请求。
And example of what I've been playing around with but I haven't had much luck:
我一直在玩的例子,但我运气不好:
@(todos: play.api.libs.json.JsValue)
@import play.api.libs.json.Json
<html>
<head>...</head>
<body>...</body>
<script>
var todos = JSON.parse(' @Json.stringify(todos) ');
</script>
</html>
Basically its spitting out a lot of quoted text to the effect of:
基本上它吐出了很多引用的文字,效果如下:
[{"id":":"294858e2-c9eb-4f50-9eac-47b257573d83"}]
Haven't had much luck with Google or the PlayFramework docs so I'd love some help.
没有太多运气与谷歌或PlayFramework文档,所以我喜欢一些帮助。
1 个解决方案
#1
8
The Play template engine will escape any strings you render to HTML, which will thoroughly mangle your JSON.
Play模板引擎将转义您呈现为HTML的任何字符串,这将彻底破坏您的JSON。
To output it verbatim, do @Html(Json.stringify(todos))
, as described here.
要逐字输出,请执行@Html(Json.stringify(todos)),如此处所述。
#1
8
The Play template engine will escape any strings you render to HTML, which will thoroughly mangle your JSON.
Play模板引擎将转义您呈现为HTML的任何字符串,这将彻底破坏您的JSON。
To output it verbatim, do @Html(Json.stringify(todos))
, as described here.
要逐字输出,请执行@Html(Json.stringify(todos)),如此处所述。