Doubt when update the view after a pattern matching

Hi! I got stuck in one point of my Elm program and need another point of view to understand what is happening behind the code.

I’ve reproduced the problem here: https://ellie-app.com/9tkdb89BdBga1 . This is an edited copy of the text-field example.

The doubt I have is I don’t know why the view is not properly refreshed after adding the pattern matching for choosing which div that should be shown:

view : Model -> Html Msg
view model =
  let
    otherDiv : Html Msg 
    otherDiv = case model.current of
      Change t -> div [name "Processed div"] [ text (String.reverse t)]
      _ -> div [name "Default div"] []
  in
  div []
    [ 
        input [ placeholder "Text to reverse", value model.content, onInput Change ] []   
        ,  otherDiv
    ]

On the presented case, it shows the default div and, after adding some text to the input, the div swap does not happen. Thus the reversed text is not shown. Why? How can I make it work?

This is a blueprint of what is happening on my code, where I choose which portion of html should be viewed depending on the last message. When the a message is produced I update the model.

you did not updating the current field:

update : Msg -> Model -> Model
update msg model =
  case msg of
    Change newContent -> 
        { model 
        | content = Just newContent 
        , current = msg
        }
    _ -> model

As a quick side note; storing the msg in the model is a code smell, but I’m guessing that just because you tried to simply the example.

Can you provide more information on why do you think you need the last message in the view?

The way things usually work is that the view generates messages, the messages update the model and the view gets updated with the new data from the model. There is no need for the view to know which was the last message.

Here is how I interpret the code you posted: https://ellie-app.com/9tzrJgRsRRca1
What is wrong with this interpretation with respect to what you are trying to achieve?

Sorry for no replying before. I detected the problem. I think I was duplicating the model, calling one function with the model and the other, the view with the pattern matching, with another modified model. Rookie mistake.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.