Op maandag 10 oktober 2016 14:58:56 UTC+2 schreef Ambrose Laing:
>
> I would modify the model to also contain a name, description and price, at 
> the top level.  These are uncommitted values.  By uncommitted I mean that 
> it should be possible to change any one of these without changing the 
> others, and yet your master list of all the items will not change because 
> you have not clicked the Run button yet.  This is because naturally it is 
> impossible to change all three items at once.  You change one, and you need 
> somewhere to hold the value you have changed while you modify the next one, 
> but before you commit a group of three of them as one article into your 
> master list.  
>

So your model would then look something like this:

type alias Model = 
  { articles : List Article
  , newName : String
  , newDescription : String
  , newPrice : String
  }


 
Your update would then include something like:

case msg of
  UpdateName inputString ->
    { model | newName = inputString }

  UpdatePrice inputString ->
     -- etcetera

Also, probably good to render these values in your view too:

[ input [ type' "text", placeholder "Enter a name...", onInput UpdateName, 
value model.newName ][]



It is only when you click Run that these three values are copied into an 
> Article and the article is appended to the List of articles in your model.
>

And the update for Run then becomes something like

case msg of
  Run ->
    let
      newID = 
        List.map (.id) model.articles -- get all IDs from the article list
        |> List.maximum -- extract the maximum value (this is a Maybe type, 
because the list could be empty)
        |> Maybe.withDefault 0 -- so give it a default value of 0
      newArticle = { id = newId, name = model.newName, ... (etcetera) }
    in
      { model
      | articles = newArticle :: model.articles
      , newName = "" -- reset the input fields after adding
      , ... (etcetera)
      }

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to