【发布时间】:2018-01-19 14:48:43
【问题描述】:
我还是 scala 的新手,还不太了解 Futures/Maps/Flatmaps/Seq 的概念以及如何正确使用它们。
这就是我想做的(伪代码):
def getContentComponents: Action[AnyContent] = Action.async {
contentComponentDTO.list().map( //Future[Seq[ContentComponentModel]] Get all contentComponents
contentComponents => contentComponents.map( //Iterate over [Seq[ContentComponentModel]
contentComponent => contentComponent.typeOf match { //Match the type of the contentComponent
case 1 => contentComponent.pictures :+ contentComponentDTO.getContentComponentPicture(contentComponent.id.get) //Future[Option[ContentComponentPictureModel]] add to _.pictures seq
case 2 => contentComponent.videos :+ contentComponentDTO.getContentComponentVideo(contentComponent.id.get) //Future[Option[ContentComponentVideoModel]] add to _.videos seq
}
)
Ok(Json.toJson(contentComponents)) //Return all the contentComponents in the end
)
}
我想像这样将Future[Option[Foo]] 添加到contentComponent.pictures: Option[Seq[Foo]]:
case 2 => contentComponent.pictures :+ contentComponentDTO.getContentComponentPicture(contentComponent.id.get) //contentComponent.pictures is Option[Seq[Foo]]
最后通过json将整个contentComponent返回给前端。
我知道这可能与最终的实际代码相去甚远,但我希望你明白这一点。谢谢!
【问题讨论】:
-
问题太宽泛/不清楚
-
请您也尽自己的一份力,想出一个更简单的例子。这是一个非常简单的问题,但您的示例增加了许多不相关的复杂性。
-
哦,好吧,我想你可能需要上下文。将编辑
-
@cchantep 这有帮助吗?你对我有什么期望?我真的不能把它归结为更多。
标签: scala playframework playframework-2.0