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

  • 本文关键字:函数 一个 消息 Elm elm
  • 更新时间 :
  • 英文 :


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

像这样:

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

如何一次激活它们?

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

简而言之,您可以制作一个调用其他子消息的单个父消息。 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

虽然我并不是说在手头的情况下这样做是一件好事(逻辑应该驻留在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)
        ]

看到这里!

最新更新