有条件地更改更新中的记录

发布于 2025-01-12 00:29:48 字数 1307 浏览 1 评论 0原文

我希望对 Msg 进行一些逻辑处理,并根据结果以不同的方式更新视图。

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

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.

I would like to have some logic worked upon the Msg and, depending on the result, update the view in a different ways.

I'm flipping some cards, and I want to test two of the selected ones. Then, accept them as a pair or discard and try again.

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

But, seems like I can't insert the logic there. Where should I put it, instead?

-- 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩人痒 2025-01-19 00:29:48

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

您可以执行以下操作。

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

The record update syntax doesn't work like that.

You can do the following.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文