查看多个textareas,如何识别哪些被修改?

时间:2018-02-11 17:51:19

标签: elm

我在Elm中创建了一个包含多个textarea元素的简单页面。我正在努力保存数据,特别是确定哪些textarea已更新。也许一个例子说明了更好的观点

我从列表中查看了多个元素

type alias Model = { List Comment, ... }

type alias Comment = {id: Int, content: String, draftContent: String, ...}

type Event = Save Comment | SaveDraft String

-- view
model.comments
|> List.map(     --iterate the list of comments and render html 
div [attrubute "name" "comment"] [
  textarea [onInput SaveDraft] [text comment.content],
  button [onClick (Save comment)] [text "Post comment"]
]

-- update
case event of
  Save comment ->
    -- Replace the comment content with draft data and clear draft

  SaveDraft draftText ->
     -- Update the draft content with text from event
     -- Which Comment is it?

基于示例here我提出了将每个textarea输入作为事件发送到更新功能并保存草稿数据的想法。

现在问题是onInput只接受带有String参数的类型,我无法识别哪条评论被修改。

1 个答案:

答案 0 :(得分:5)

更改Event联合类型以包含评论(SaveDraft String - > SaveDraft Comment String

type Event = Save Comment | SaveDraft Comment String

-- view
model.comments
|> List.map(     --iterate the list of comments and render html 
div [attrubute "name" "comment"] [
  textarea [onInput (SaveDraft comment.content)] [text comment.content],
  button [onClick (Save comment)] [text "Post comment"]
]

Currying使(SaveDraft comment.content)具有与之前相同的返回值