我有一种情况,我有两种方法可以在Elm应用程序中播放音符,并且我会记录当前正在播放的音符。目前的代码在这里:
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
PlayNote note ->
let
updatedCurrentPlaying =
note :: model.currentPlaying
in
( { model | currentPlaying = updatedCurrentPlaying }, Cmd.none )
KeyDown keyCode ->
let
note =
List.filter (\x -> x.keyCode == keyCode) model.notes
|> List.head
updatedCurrentPlaying =
case note of
Nothing ->
model.currentPlaying
Just a ->
a :: model.currentPlaying
in
( { model | currentPlaying = updatedCurrentPlaying }, Cmd.none )
我想知道的是,无论如何都要干这一点,并导致KeyDown
案例触发PlayerNote note
消息而不是复制功能。我尝试用涉及任务的内容替换Cmd.none
并直接调用更新,但它似乎无法正常工作。
我是以完全错误的方式来做这件事的吗?这不是榆树真正允许的吗?
答案 0 :(得分:8)
回想一下,update
只是一个函数,可以像任何其他函数一样调用。您可以通过递归方式调用PlayNote
Msg
:
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
PlayNote note ->
let
updatedCurrentPlaying =
note :: model.currentPlaying
in
( { model | currentPlaying = updatedCurrentPlaying }, Cmd.none )
KeyDown keyCode ->
let
note =
List.filter (\x -> x.keyCode == keyCode) model.notes
|> List.head
in
case note of
Nothing ->
( model, Cmd.none )
Just a ->
update (PlayNote a) model