如何在Elm 0.18中的一个函数中触发多条消息?

Kno*_*our 4 elm

我想在订阅时调整窗口大小时触发多条消息.

就像是:

subscription : Model -> Sub Msg
subscription model =
  Window.resizes (\{width, height} ->
    Sidebar "hide"
    Layout "card"
    Search <| Name ""
    Screen width height
  )
Run Code Online (Sandbox Code Playgroud)

我如何一次激活它们?

Tos*_*osh 6

我也有兴趣看看其他人会回答什么.但这就是我要做的.

简而言之,您可以创建一个调用其他子消息的单个父消息. andThen函数只是有助于连接更新调用.

andThen : Msg -> ( Model, Cmd msg ) -> ( Model, Cmd msg )
andThen msg ( model, cmd ) =
    let
        ( newmodel, newcmd ) =
            update msg model
    in
        newmodel ! [ cmd, newcmd ]


update : Msg -> Model -> ( Model, Cmd msg )
update msg model =
    case Debug.log "message" msg of
        DoABC ->
            update DoA model
                |> andThen DoB
                |> andThen DoC
Run Code Online (Sandbox Code Playgroud)


Adr*_*ian 5

虽然我没有说在手头的情况下这是一件好事(逻辑应该存在于update函数中),但你可以通过批处理这样的信号列表来做到这一点:

subscription : Model -> Sub Msg
subscription model =
    Sub.batch 
        [ Window.resizes (\_ -> Sidebar "hide")
        , Window.resizes (\_ -> Layout "card")
        , Window.resizes (\_ -> Search <| Name "")
        , Window.resizes (\{width, height} -> Screen width height)
        ]
Run Code Online (Sandbox Code Playgroud)

看到这个!