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

时间:2017-02-05 06:09:39

标签: elm

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

类似的东西:

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

我如何一次激活它们?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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)
        ]

请参阅this

相关问题