0

我正在为我必须使用 Scala 的视图开发一个带有 play framework v 2.1 的
应用 程序.x/Samples the Forms one
在应用程序的views文件夹中有一个名为form.scala.html的文件views.contact包

@(contactForm: Form[Contact])
@import helper._
@import helper.twitterBootstrap._

 @title = {
 Add a new contact <small><a href="@routes.Contacts.edit">Or edit an existing     contact</a></small>
 }

@phoneField(field: Field, className: String = "phone") = {
    @input(field, '_label -> "Phone numbers", '_class -> className) { (id, name, value, _) =>
    <input type="text" name="@name" value="@value"> 
    <a class="removePhone btn danger">Remove</a>
    }
}

@informationGroup(field: Field, className: String = "profile") = {
<div class="twipsies well @className">

    <a class="removeProfile btn danger pull-right">Remove this profile</a>

    @inputText(
        field("label"), 
        '_label -> "Label"
    )

    @inputText(
        field("email"), 
        '_label -> "Email"
    )

    <div class="phones">

        @repeat(field("phones"), min = 0) { phone =>

            @phoneField(phone("number"))

        }

        @**
         * Keep an hidden block that will be used as template for Javascript copy code
         **@
        @phoneField(
            field("phones[x].number"),
            className = "phone_template"
        )

        <div class="clearfix">
            <div class="input">
                <a class="addPhone btn success">Add a phone number</a>
            </div>
        </div>

    </div>

</div>
}

@main(title, nav = "contact") {

@if(contactForm.hasErrors) {
    <div class="alert-message error">
        <p><strong>Oops</strong> Please fix all errors</p>
    </div>
}

@helper.form(action = routes.Contacts.submit, 'id -> "form") {

    <fieldset>
        <legend>General informations</legend>

        @inputText(
            contactForm("firstname"), 
            '_label -> "First name"
        )

        @inputText(
            contactForm("lastname"), 
            '_label -> "Last name"
        )

        @inputText(
            contactForm("company"), 
            '_label -> "Company"
        )

    </fieldset>

    <fieldset>
        <legend>Profiles</legend>

        <div id="profiles">

            @repeat(contactForm("informations")) { information =>

                @informationGroup(information)

            }

            @**
             * Keep an hidden block that will be used as template for Javascript copy code
             **@
            @informationGroup(
                contactForm("informations[x]"),
                className = "profile_template"
            )

            <div class="manage">
                <a class="addProfile btn success">Add another profile</a>
            </div>

        </div>

    </fieldset>

    <div class="actions">
        <input type="submit" class="btn primary" value="Insert">
        <a href="@routes.Application.index" class="btn">Cancel</a>
    </div>

}

该代码应该呈现这样的视图
在此处输入图像描述

通过按添加电话号码,一些文件被添加到表单中,它会变成这样:

在此处输入图像描述

让我对这段代码真正感到困惑的是这些部分以及它们是如何工作的:

@phoneField(field: Field, className: String = "phone") = {
    @input(field, '_label -> "Phone numbers", '_class -> className) { (id, name, value,   _) =>
  <input type="text" name="@name" value="@value"> 
  <a class="removePhone btn danger">Remove</a>
  }

}

@repeat(field("phones"), min = 0) { phone =>

        @phoneField(phone("number"))

    }

    @**
     * Keep an hidden block that will be used as template for Javascript copy code
     **@
    @phoneField(
        field("phones[x].number"),
        className = "phone_template"
    )

有人可以向我简要解释一下这些代码行的工作原理吗,请不要将博客或网站上的简短教程链接放在 Scala 我可以通过 Google 搜索自己找到

我只是在寻找关于这些代码行的简短但描述性的解释,在此先感谢!

顺便说一句,我从原始代码中删除了 javascript 代码

4

1 回答 1

2

让我们从@phoneField函数开始:

@phoneField(field: Field, className: String = "phone") = {
    @input(field, '_label -> "Phone numbers", '_class -> className) { (id, name, value,   _) =>
       <input type="text" name="@name" value="@value"> 
       <a class="removePhone btn danger">Remove</a>
    }
}

@input是一个帮助器(即函数),允许您自己为该字段创建 html。在这种情况下需要这样做,因为我们要添加.removePhone按钮。因此,@phoneField只需获取实例Field并构造 html 输入和删除链接。

现在,怎么样@repeat

@repeat(field("phones"), min = 0) { phone =>            
     @phoneField(phone)
}

在 app/controllers/Contacts.scala 定义的contactForm 中,您可以看到“电话”字段定义为列表(文本)。它是一种包含文本字段元素的集合。所以@repeat 将遍历field("phones")并将每个文本字段传递给@phoneField. 重要的是,所有将转到@phoneField 的字段都将具有类似“phones[0]”、“phones 1 ”、...的名称。

现在事情变得有趣了。

 @phoneField(
       field("phones[x]"),
       className = "phone_template"
 )

为 javascript 函数构造一个模板,该模板将在响应“添加电话字段”按钮时将其内容复制到页面。看起来像field("phones[x]")构造了名称为“phones[x]”的空字段,类似于@repeat生成的内容。然后整个构造将创建一个名为“phones[x]”和空值的电话字段(和删除链接)。

当您查看 javascript 代码时,您会看到当用户单击“添加电话号码”链接时,将执行 javascript 回调,它将 html 从模板复制到 dom 下<div class="phones">,并将重新编号所有 .phone 输入哪个名称火柴/phones\[.+\]/

我希望您已阅读使用表单模板助手

于 2014-01-16T09:59:14.533 回答