Is it correct way to get DOM to function?

105 views
Skip to first unread message

Юрий Яковенко

unread,
May 30, 2016, 4:55:29 PM5/30/16
to Elm Discuss
Hello all

Sorry i'm new in Elm, could you clarify

I've made exercise in checkboxes example, please review it, Am i in right way?

import Html exposing (..)
import Html.App exposing (beginnerProgram)
import Html.Attributes exposing (..)
import Html.Events exposing (onCheck)

main
=
  beginnerProgram
{ model = model, view = view, update = update }

-- MODEL

type
alias Model =
 
{ red : Bool
 
, underline : Bool
 
, bold : Bool
 
}

model
: Model
model
=
 
Model False False True

-- UPDATE

type
Msg
 
= Red Bool
 
| Underline Bool
 
| Bold Bool

update
: Msg -> Model -> Model
update msg model
=
 
case msg of
   
Red bool ->
       
{ model | red = bool }

   
Underline bool ->
       
{ model | underline = bool }

   
Bold bool ->
       
{ model | bold = bool }

-- VIEW

view
: Model -> Html Msg
view model
=
  let
    myLabel
: Bool -> ( Bool -> Msg ) -> Html Msg
    myLabel newChecked
event =
      label
[]
       
[ br [] []
       
, input [ type' "checkbox", checked newChecked, onCheck event ] []
        , text "red"
        ]
  in
    div []
      [ span [toStyle model] [ text "Hello, how are you?!"]
      , myLabel model.red Red
      , myLabel model.underline Underline
      , myLabel model.bold Bold
      ]


toStyle : Model -> Attribute msg
toStyle model =
  style
    [ ("color", if model.red then "red" else "black")
    , ("text-decoration", if model.underline then "underline" else "none")
    , ("font-weight", if model.bold then "bold" else "normal")
    ]

-- Exercise: move the repetative code in `view` into a separate function
-- and use it three times.

Gage Peterson

unread,
May 31, 2016, 1:49:13 PM5/31/16
to Elm Discuss
I've just tried running your code file and it seems to work. Were you trying to accomplish something specific?

Юрий Яковенко

unread,
Jun 1, 2016, 4:21:21 AM6/1/16
to Elm Discuss
I've add myLabel function to avoid repeating of template code, just wanted to ask, is correct way to do this?


вт, 31 мая 2016 г. в 20:49, Gage Peterson <just...@gmail.com>:
I've just tried running your code file and it seems to work. Were you trying to accomplish something specific?

--
You received this message because you are subscribed to a topic in the Google Groups "Elm Discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elm-discuss/G9YeVjeBRAE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elm-discuss...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alexandre Galays

unread,
Jun 1, 2016, 6:48:40 AM6/1/16
to Elm Discuss
I would generally move the function definition out of the view for performance reasons, as it gets redefined on every render in your example.

Юрий Яковенко

unread,
Jun 1, 2016, 9:31:32 AM6/1/16
to Elm Discuss
Understood, Thank you)

ср, 1 июн. 2016 г. в 13:48, Alexandre Galays <a...@zenexity.com>:
Reply all
Reply to author
Forward
0 new messages