gpt4 book ai didi

elm - 处理 elm 中的大量输入字段是否有一个很好的模式?

转载 作者:行者123 更新时间:2023-12-04 16:03:56 27 4
gpt4 key购买 nike

elm 中是否有一种模式可以避免编写大量消息只是为了更新模型子元素上的各个字段?

目前,我最终得到如下代码,每个更改的输入都有一条消息,然后是每个字段的一堆更新逻辑。我想要做的是有一个像 AChanged 这样的消息来处理对 A 的任何属性的所有更改。要么通过更新生成消息的函数中的记录,要么传入一个字段名称,然后使用它直接执行像在 Javascript 中一样更新记录。

module Main exposing (Model)

import Browser exposing (Document, UrlRequest)
import Browser.Navigation as Nav exposing (Key)
import Html exposing (div, input)
import Html.Events exposing (onInput)
import Url exposing (Url)


type alias A =
{ a : String
, b : String
, c : String
, d : String
}


type alias B =
{ e : String
, f : String
, g : String
, h : String
}


type alias Model =
{ key : Nav.Key
, url : Url.Url
, a : A
, b : B
}


type Msg
= UrlChanged Url.Url
| LinkClicked Browser.UrlRequest
| AaChanged String
| AbChanged String
| AcChanged String
| AdChanged String
| BeChanged String
| BfChanged String
| BgChanged String
| BhChanged String


init : () -> Url.Url -> Nav.Key -> ( Model, Cmd Msg )
init flag url key =
( Model key url (A "" "" "" "") (B "" "" "" ""), Cmd.none )


subscriptions : Model -> Sub Msg
subscriptions _ =
Sub.none


view : Model -> Document msg
view model =
{ title = "Mister Mandarin"
, body =
div
[ input [ onInput AaChanged ] []
, input [ onInput AbChanged ] []
, input [ onInput AcChanged ] []
, input [ onInput AdChanged ] []
, input [ onInput BeChanged ] []
, input [ onInput BfChanged ] []
, input [ onInput BgChanged ] []
, input [ onInput BhChanged ] []
]
[]
}


update : Msg -> Model -> ( Model, Cmd msg )
update msg model =
case msg of
LinkClicked urlRequest ->
case urlRequest of
Browser.Internal url ->
( model, Nav.pushUrl model.key (Url.toString url) )

Browser.External href ->
( model, Nav.load href )

UrlChanged url ->
( { model | url = url }
, Cmd.none
)

AaChanged value ->
let
a =
model.a

newA =
{ a | a = value }
in
( { model | a = newA }, Cmd.none )

AbChanged value ->
let
a =
model.a

newA =
{ a | b = value }
in
( { model | a = newA }, Cmd.none )

AcChanged value ->
let
a =
model.a

newA =
{ a | c = value }
in
( { model | a = newA }, Cmd.none )

AdChanged value ->
let
a =
model.a

newA =
{ a | d = value }
in
( { model | a = newA }, Cmd.none )

BeChanged value ->
let
b =
model.b

newB =
{ b | e = value }
in
( { model | b = newB }, Cmd.none )

BfChanged value ->
let
b =
model.b

newB =
{ b | f = value }
in
( { model | b = newB }, Cmd.none )

BgChanged value ->
let
b =
model.b

newB =
{ b | g = value }
in
( { model | b = newB }, Cmd.none )

BhChanged value ->
let
b =
model.b

newB =
{ b | h = value }
in
( { model | b = newB }, Cmd.none )


main : Program () Model Msg
main =
Browser.application
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
, onUrlChange = UrlChanged
, onUrlRequest = LinkClicked
}

最佳答案

我对这个问题采取了两种截然不同的方法。给您最大控制权(同时仍然有助于消除冗长)的方法是将您的逻辑从 Update 移开。给您的 View用广义 Msg .类似:UpdateForm (String -> Model)UpdateForm (String -> FormModel) .

另一种方法是完全放弃在模型中存储输入状态。这样做的缺点是不允许您执行诸如初始化输入或轻松清除输入之类的操作。但它作为一种快速而肮脏的方法来获取基本表格是很棒的。在此方法中,您可以利用带有 name 的输入元素这一事实。属性成为父级的属性 form元素1。您可以将解码器附加到表单的 onSubmit并通过 Decode.at ["ab", "value"] 获取值.

1 https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-name

关于elm - 处理 elm 中的大量输入字段是否有一个很好的模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55216298/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com