我在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
参数的类型,我无法识别哪条评论被修改。
答案 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)
具有与之前相同的返回值