簡體   English   中英

Scala函數如何在Play框架2.1中工作

[英]How Scala functions work in Play framework 2.1

我正在開發一個帶有play framework v 2.1的應用程序,用於我必須使用Scala的視圖
我試圖閱讀一個播放框架的示例代碼,可以在這里找到http://www.playframework.com/documentation/2.0.x/Samples 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代碼

讓我們從@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因為我們要添加.removePhone按鈕。 所以@phoneField只需要獲取Field實例並構造html輸入和remove-link。

現在, @repeat怎么樣?

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

在app / controllers / Contacts.scala中定義了contactForm,在那里你可以看到“phones”字段定義為list(text)。 它是一種帶有文本字段元素的集合。 因此@repeat將遍歷field("phones")並將每個文本字段傳遞給@phoneField 重要的是,所有將轉到@phoneField的字段都會有“phone [0]”,“phone 1 ”等名稱....

現在事情變得有趣了。

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

構建javascript函數的模板,該模板將響應“添加電話字段”按鈕將其內容復制到頁面。 看起來像field("phones[x]")構造名為“phones [x]”的空字段,類似於@repeat正在生成的@repeat 然后整個構造將創建一個名為“phones [x]”和空值的電話字段(和刪除鏈接)。

當您查看javascript代碼時,您會看到當用戶點擊“添加電話號碼”鏈接時,將執行javascript回調,將html從模板復制到<div class="phones">下的dom,並且將重新編號所有的.phone輸入名稱匹配/ /phones\\[.+\\]/

我希望您已閱讀使用表單模板助手

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM