【问题标题】:Play 2.0 - auxiliary constructors in templatesPlay 2.0 - 模板中的辅助构造函数
【发布时间】:2012-04-22 19:19:26
【问题描述】:

Play 2.0 模板中是否可以有辅助构造函数?

【问题讨论】:

    标签: scala playframework-2.0


    【解决方案1】:

    我认为“构造函数”是指具有不同参数的参数列表。我不知道执行此操作的内置方法,但我才刚刚开始学习 Play。

    但是您可以使用 Enhance My Instance™ 模式来实现相同的效果:

    使用to-do list example,假设您的index.scala.html 模板开始:

    @(tasks: List[Task], taskForm: Form[String])
    

    Application.scala 中,您使用

    调用它
      def tasks    = Action { Ok(views.html.index(Task.all(), taskForm)) }
    

    如果您想省略任务列表:

      implicit def enhanceIndex(index: views.html.index.type) = new {
        def apply(f: Form[String]) = index(List.empty, f)
      }
    

    现在你可以这样称呼它:

      def tasks2   = Action { Ok(views.html.index(taskForm)) }
    

    这本质上只是使用 .type 将范围缩小到特定实例的 pimp-my-library 模式,在本例中是 views.html.index 对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-10
      • 1970-01-01
      • 2015-01-28
      • 1970-01-01
      • 2014-08-20
      • 1970-01-01
      相关资源
      最近更新 更多