gdotdesign / elm-ui

UI library for making web applications with Elm
https://elm-ui.netlify.com
BSD 2-Clause "Simplified" License
920 stars 39 forks source link

Confused about the Model.... #70

Closed nickperkinslondon closed 7 years ago

nickperkinslondon commented 7 years ago

Suppose I have a List of Things...

type alias Thing = {
  name : String,
  active : Bool
}

...and I want to render each thing with a Checkbox... The documentation says that I have to put a specific type of data on my model, like this:

type alias Thing = {
  name : String,
  active : Ui.Checkbox.Model
}

That feels strange! I was hoping to keep my Model abstract, and free of any UI dependencies! Why can't my model just use a Bool? Am I doing it wrong?

gdotdesign commented 7 years ago

Well if you check the the source code of Ui.Checkbox (https://github.com/gdotdesign/elm-ui/blob/development/source/Ui/Checkbox.elm#L37) you can see it's not just boolean value, it can be disabled or readonly so it has it's own state, also it's inline with some of the more complex components like Ui.Calendar or Ui.Chooser in terms of offering the same API (the traditional TEA).

nickperkinslondon commented 7 years ago

So, in Elm, a "component" cannot hold it's own state? Which means we have to "host" its state somewhere within our app state, right? This seems more awkward than what I was doing in React. For me and my "List of Things", I ended up having to "bake-in" a Checkbox.Model constructor into my JSON decoder, which doesn't feel right -- I would hope that a UI library would only affect the "view" function. Again, maybe I am doing it wrong -- maybe there is some way to separate UI state from "business" state I will keep trying -- this UI looks great -- thanks for your help

nickperkinslondon commented 7 years ago

BTW....what is "the traditional TEA" ? ( i tried to google it, but you can imagine what i got...)

gdotdesign commented 7 years ago

I'm talking about the first "The Elm Architecture" (TEA) that was introduced to encapsulate logic into modules where each module consists of:

All components in Elm-UI follows this structure.

This official TEA has been changed recently to this: https://github.com/evancz/elm-sortable-table#the-elm-architecture - moving the state around in messages (which I don't agree with mainly because in certain cases it can lead to race conditions)