On Wednesday, January 25, 2017 at 12:11:46 PM UTC, Rupert Smith wrote:
>
> I think a better approach would be to put build a TEA component like this:
>
> module MyComponent exposing (Model, Msg, update, view, init,
> subscriptions) -- and perhaps also OutMsg
>
So I think I understand where the tempation to put Model and Msg in
Types.elm comes from. As the codebase grows I may end up with lots of
functions for rendering the view, which are of the type Model -> Html Msg.
Or it may be the update function that grows and I end up with lots of
helper update functions of type a -> (Model, Cmd.Msg). So these functions
cannot simply be pushed down to a sub-module as they need to import the
type, but that is in the main module that is using them - a circular
dependency.
Just grabbing some random function from view code I was working on today:
slideButton : Model -> Html Msg
slideButton model =
div
[ class "slide-button"
, Events.onClick ToggleMenu
]
[ div
(Animation.render model.slideButtonStyle
++ [ class "slide-button__inset" ]
)
[]
]
I could push this down into a child module by not passing the whole Model,
but just the bits it needs, and by passing a 'tagger' for building the
messages:
slideButton : Animation.State -> msg -> Html msg
slideButton slideButtonStyle clickTagger =
div
[ class "slide-button"
, Events.onClick clickTagger
]
[ div
(Animation.render slideButtonStyle
++ [ class "slide-button__inset" ]
)
[]
]
Interestingly by removing the Msg and Model type from this piece of view
logic I have also made it re-usable (with other Models and Msgs, that is,
other components). So I think if I structure my code this way, as groups of
related functionality emerge and files get too long, the tidying up
activity will be to introduce re-usability into code as it it organized.
So some good rules about modules with Types only might be:
* A module that declares Type only cannot take responsibility for
implementing some functionality, so modules like this should be avoided.
* A module that declares Type only can introduce too much coupling when it
is used as a technique to break the circular dependency of modules.
--
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.