参数,Html复选框采用elm

时间:2015-07-28 15:05:34

标签: elm

这是取自elm复选框examples -

的视图片段
view address model =
  div [] <|
    span [toStyle model] [text "Hello, how are yo?"]
    :: br [] []
    :: checkbox address model.red Red "red"
    ++ checkbox address model.underline Underline "underline"
    ++ checkbox address model.bold Bold "bold"

checkbox : Address Action -> Bool -> (Bool -> Action) -> String -> List Html
checkbox address isChecked tag name =
  [ input
      [ type' "checkbox"
      , checked isChecked
      , on "change" targetChecked (Signal.message address << tag)
      ]
      []
  , text name
  , br [] []
  ]

1)据我所知,double colonsdouble plus用于连接列表?它们彼此有什么不同?

2)在此行的checkbox函数(Signal.message address << tag)中,tag取消绑定的内容是什么?是Red(或)red吗?这个论点用于什么?

3)address函数的参数类型是什么?

1 个答案:

答案 0 :(得分:2)

Answer

  1. double colon (::) adds a single element to the start of a list.
    double plus (++) concatenates two lists.
  2. tag would be equal to Red in the when checkbox is called from checkbox address model.red Red "red". Red is a function from Bool to Action. It wraps the event of the check box in this data constructor so that later in the update function, you can distinguish the event of that check box from the events of the other check boxes.
  3. address is not a function. It has type Address Action, meaning it holds the address of a mailbox that can receive Action messages. This "mailbox" is not visible in the code, it's used internally by StartApp, which is used in the main function.

Code references

To keep this question useful even if the linked example changes, these are the relevant code portions I'm referring to:

The update function:

update action model =
  case action of
    Red bool ->
        { model | red <- bool }

    Underline bool ->
        { model | underline <- bool }

    Bold bool ->
        { model | bold <- bool }

The Action type:

type Action
  = Red Bool
  | Underline Bool
  | Bold Bool

The main function:

main =
  StartApp.start { model = initialModel, view = view, update = update }