TedTheGuy
TedTheGuy

Reputation: 129

How to update a field/value inside a model?

If my model looks like:

type alias Application = { id : Int , term : Int , amount : Int }  
type alias Model = { application : Application }

and I am trying to update the term value, I have onInput UpdateTerm on an input inside my update case statement how do I update this value?

so far I have UpdateTerm term ->; but unsure how I can update only the term value inside application?

Upvotes: 2

Views: 108

Answers (2)

Mike Whittom
Mike Whittom

Reputation: 77

for a better readability you can make function like this:

update msg model = 
    case msg of
       changeNestedProperty property -> 
            ({ model | record= setNestedProperty property model.record } , Cmd.none)

setNestedProperty : String -> Record-> Record
setNestedProperty property record = 
   { record | nestedProperty = property }

Upvotes: 1

glennsl
glennsl

Reputation: 29106

Record field update is described in the guide and also in the reference documentation. Updating a field in a record nested inside another record is simply a matter of doing one after the other. Assuming you have a binding named model:

let
    application =
        model.application

    updatedApplication =
        { application | term = term }
in
{ model | application = updatedApplication }

Upvotes: 6

Related Questions