Vlad GURDIGA
2017-08-24 07:10:07 UTC
Hey Elm-guys and Elm-gals! ð
I have this toy-project
<https://github.com/gurdiga/xo.elm/tree/9fe9bd1b9f7cb13b6653ec55d9e873033e8930ed>
where Iâm getting my feet wet with Elm, and Iâve found an approach to
compose view function thatâs a bit different than what The Elm Architecture
recommends. ð€
Because I found message transformation (mapping
<http://package.elm-lang.org/packages/elm-lang/html/2.0.0/Html#map>)
between nested components to be counterintuitive, I left out messages, for
the most part. I only have one
<https://github.com/gurdiga/xo.elm/blob/9fe9bd1b9f7cb13b6653ec55d9e873033e8930ed/src/Main.elm#L33-L41>
for the whole app: ð
type Msg
= Update Model (Cmd Msg) (Sub Msg)
update : Msg -> Model -> ( Model, Cmd Msg )update msg model =
case msg of
Update model cmd sub ->
( { model | subscription = sub }, cmd )
Only the top component references it
<https://github.com/gurdiga/xo.elm/blob/9fe9bd1b9f7cb13b6653ec55d9e873033e8930ed/src/Main.elm#L64>,
and from then on I use callback-style functions
<https://github.com/gurdiga/xo.elm/blob/9fe9bd1b9f7cb13b6653ec55d9e873033e8930ed/src/Dosar.elm#L31-L42>
for wiring up the nested components:
view : Dosar -> (Dosar -> Cmd msg -> Sub msg -> msg) -> Html msgview (Dosar data) callback =
let
c data =
callback (Dosar data)
in
div []
[ h1 [] [ text "Dosar nou" ]
, Temei.view data.temei (\v -> c { data | temei = v })
, DocumentExecutoriu.view data.documentExecutoriu (\v -> c { data | documentExecutoriu = v } Cmd.none Sub.none)
, Actiune.view data.actiune (\v -> c { data | actiune = v })
]
and also for event handlers
<https://github.com/gurdiga/xo.elm/blob/9fe9bd1b9f7cb13b6653ec55d9e873033e8930ed/src/Widgets/Fields.elm#L27-L34>
:
unlabeledTextField : String -> (String -> msg) -> List (Html msg)unlabeledTextField defaultValue callback =
[ input
[ value defaultValue
, onInput callback
]
[]
]
It seems to work well so far, and coming from JS I find this style a bit
more familiar conceptually: I only have functions and values. ð€
I am at about 184K of Elm now, and Iâm wondering if anyone else have tried
to do it this way and maybe found some gotchas that I should be aware of.
In particular, with regards to how the code compiles to JS and maybe other
Elmâs inner workings. ð€
Cheers! ð€
(NOTE: If you want to browse the code, please use this
revision: https://github.com/gurdiga/xo.elm/tree/9fe9bd1. After that Iâm
bringing in elm-mdl
<http://package.elm-lang.org/packages/debois/elm-mdl/8.1.0>, and the code
will probably get too noisy for the purpose of this conversation. ð)
I have this toy-project
<https://github.com/gurdiga/xo.elm/tree/9fe9bd1b9f7cb13b6653ec55d9e873033e8930ed>
where Iâm getting my feet wet with Elm, and Iâve found an approach to
compose view function thatâs a bit different than what The Elm Architecture
recommends. ð€
Because I found message transformation (mapping
<http://package.elm-lang.org/packages/elm-lang/html/2.0.0/Html#map>)
between nested components to be counterintuitive, I left out messages, for
the most part. I only have one
<https://github.com/gurdiga/xo.elm/blob/9fe9bd1b9f7cb13b6653ec55d9e873033e8930ed/src/Main.elm#L33-L41>
for the whole app: ð
type Msg
= Update Model (Cmd Msg) (Sub Msg)
update : Msg -> Model -> ( Model, Cmd Msg )update msg model =
case msg of
Update model cmd sub ->
( { model | subscription = sub }, cmd )
Only the top component references it
<https://github.com/gurdiga/xo.elm/blob/9fe9bd1b9f7cb13b6653ec55d9e873033e8930ed/src/Main.elm#L64>,
and from then on I use callback-style functions
<https://github.com/gurdiga/xo.elm/blob/9fe9bd1b9f7cb13b6653ec55d9e873033e8930ed/src/Dosar.elm#L31-L42>
for wiring up the nested components:
view : Dosar -> (Dosar -> Cmd msg -> Sub msg -> msg) -> Html msgview (Dosar data) callback =
let
c data =
callback (Dosar data)
in
div []
[ h1 [] [ text "Dosar nou" ]
, Temei.view data.temei (\v -> c { data | temei = v })
, DocumentExecutoriu.view data.documentExecutoriu (\v -> c { data | documentExecutoriu = v } Cmd.none Sub.none)
, Actiune.view data.actiune (\v -> c { data | actiune = v })
]
and also for event handlers
<https://github.com/gurdiga/xo.elm/blob/9fe9bd1b9f7cb13b6653ec55d9e873033e8930ed/src/Widgets/Fields.elm#L27-L34>
:
unlabeledTextField : String -> (String -> msg) -> List (Html msg)unlabeledTextField defaultValue callback =
[ input
[ value defaultValue
, onInput callback
]
[]
]
It seems to work well so far, and coming from JS I find this style a bit
more familiar conceptually: I only have functions and values. ð€
I am at about 184K of Elm now, and Iâm wondering if anyone else have tried
to do it this way and maybe found some gotchas that I should be aware of.
In particular, with regards to how the code compiles to JS and maybe other
Elmâs inner workings. ð€
Cheers! ð€
(NOTE: If you want to browse the code, please use this
revision: https://github.com/gurdiga/xo.elm/tree/9fe9bd1. After that Iâm
bringing in elm-mdl
<http://package.elm-lang.org/packages/debois/elm-mdl/8.1.0>, and the code
will probably get too noisy for the purpose of this conversation. ð)
--
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 elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
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 elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.