更新中有条件地更改记录

Conditionally change record in update

我想对 Msg 进行一些逻辑处理,并且根据结果,update 以不同的方式查看视图。

我正在翻牌,我想测试其中的两张。然后,将它们作为一对接受或丢弃并重试。

update : Msg -> Model -> Model
update msg model =
    case msg of
        ClickedCard data ->
            { model
                | activeCard = data.id
                , (if List.lenght selectedCards < 2 then
                      selectedCards = data.id :: model.selectedCards
                  else if (List.take 1 model.selectedCards) == (List.drop 1 model.selectedCards) then
                           completedPairs = ( List.take 1 model.selectedCards , List.drop 1 model.selectedCards ):: model.completedPairs
                          else
                              selectedCards = List.drop 2 model.selectedCards)
            }

        _ ->
            model

但是,好像我不能在那里插入逻辑。我应该把它放在哪里?

-- PROBLEM IN RECORD ------------------------------------------ src/Flipping.elm

I am partway through parsing a record, but I got stuck here:

126|             { model
127|                 | activeCard = data.id
128|                 , (if List.lenght selectedCards < 2 then
                       ^
I was expecting to see another record field defined next, so I am looking for a
name like userName or plantHeight.

记录更新语法不是那样工作的。

您可以执行以下操作。

update : Msg -> Model -> Model
update msg model =
    case msg of
        ClickedCard data ->
            let 
               newModel = { model | activeCard = data.id }
            in
              if List.length selectedCards < 2 then
               {newModel | selectedCards = data.id :: model.selectedCards}
              else if (List.take 1 model.selectedCards) == (List.drop 1 model.selectedCards) then
               {newModel | completedPairs = ( List.take 1 model.selectedCards , List.drop 1 model.selectedCards ):: model.completedPairs}
              else
               {newModel | selectedCards = List.drop 2 model.selectedCards)}

        _ ->
            model