我正在编写一个 Play 2.3.2 应用程序。
在我的 Controller 中,我有一个异步方法来计算 Future[Option[(String, Int)]]
。
由于计算速度很慢,我想开始创建 View ,并在计算完成后完成 View 的构建。
在同一个 Controller 中,我有一个调用此方法并加载传递结果的 View 的操作,如下所示:
def max = Action.async {
calculateMax.flatMap(result =>
Future{Ok(recommendationsystem.views.html.manager.statistics.max(result))
)
}
我的最大 View 是这样实现的:
@(max: scala.concurrent.Future[Option[(String, Int)]])
@import util._
@recommendationsystem.views.html.main("Max tag")(recommendationsystem.views.html.nav.navbar("statistics")) {
<h1>Most used tag</h1>
@max flatMap {result => result match {
case Some(x) => {
<p>
<ul>
<li>Tag: @x._1</li>
<li>Occurrencies: @x._2</li>
</ul>
</p>
}
case None => {
<p>No one tag present in the db</p>
}
}
}
<p><a href="@recommendationsystem.controllers.manager.routes.StatisticsController.index()"></a> </p>
}
但是这段代码无法编译,编译器给出以下编译错误:
[error] /Users/alberto/git/bdrim/modules/recommendation-system/app/recommendationsystem/views/manager/statistics/max.scala.html:8: ')' expected but 'case' found.
[error] case Some(x) => {
[error] ^
[error] /Users/alberto/git/bdrim/modules/recommendation-system/app/recommendationsystem/views/manager/statistics/max.scala.html:18: ')' expected but 'case' found.
[error] case None => {
[error] ^
[error] /Users/alberto/git/bdrim/modules/recommendation-system/app/recommendationsystem/views/manager/statistics/max.scala.html:26: ';' expected but ',' found.
[error] <p><a href="@recommendationsystem.controllers.manager.routes.StatisticsController.index()"></a> </p>
[error] ^
[error] three errors found
[error] /Users/alberto/git/bdrim/modules/recommendation-system/target/scala-2.11/twirl/main/recommendationsystem/views/html/manager/statistics/max.template.scala:27: ')' expected but 'case' found.
[error] """),_display_(/*7.6*/max),format.raw/*7.9*/(""" """),format.raw/*7.10*/("""flatMap """),format.raw/*7.18*/("""{"""),format.raw/*7.19*/("""result => result match """),format.raw/*7.42*/("""{""")/*8.5*/case Some(x) =>/*8.20*/ {_display_(Seq[Any](format.raw/*8.22*/("""
[error] ^
[error] /Users/alberto/git/bdrim/modules/recommendation-system/target/scala-2.11/twirl/main/recommendationsystem/views/html/manager/statistics/max.template.scala:36: ')' expected but 'case' found.
[error] """)))}/*18.5*/case None =>/*18.17*/ {_display_(Seq[Any](format.raw/*18.19*/("""
[error] ^
[error] /Users/alberto/git/bdrim/modules/recommendation-system/target/scala-2.11/twirl/main/recommendationsystem/views/html/manager/statistics/max.template.scala:40: ';' expected but ',' found.
[error] """)))},format.raw/*23.5*/("""}"""),format.raw/*23.6*/("""
[error]
View 似乎看不到 Future 类型。 怎么了??
最佳答案
此处的编译错误来自您的 twirl 语法,而不是 Future
。旋转模板对间距和换行非常挑剔。带有空格的 @max flatMap 肯定会破坏一些东西,因为 twirl 不知道它是否应该打印出 max,或者寻找更多代码。最好用 @{ ... }
包围整个 block 。
其中一些是无关紧要的,因为您不应该首先将 Future
传递给 View 。模板不会部分渲染,然后稍后填充 Future
值。将 recommendationsystem.views.html.manager.statistics.max
包装在 Future.apply
中基本上没有任何作用。这里要注意的关键是 result
甚至不是 Future
。假设 calculateMax
返回 Future[Option[(String, Int)]]
,当您调用 map
时,您正在异步操作 Option[(String, Int)]
Future
的结果。
所以你的 Controller 函数应该如下所示:
def max = Action.async {
calculateMax.map(result =>
Ok(recommendationsystem.views.html.manager.statistics.max(result))
)
}
你的看法是这样的:
@(max: Option[(String, Int)])
@max match {
case Some(x) => {
...
}
case None => {
<p>No one tag present in the db</p>
}
}
关于html - 在游戏中的 View 中使用 Future 2.3.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26775584/