Created
June 17, 2016 20:29
-
-
Save dela3499/aab9db488cd2f924a57d74fe6a8c8060 to your computer and use it in GitHub Desktop.
Example use of Elm 0.17 - taking advantage of subscriptions, and tasks.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Html.App as Html | |
import Html exposing ( Html, div, text ) | |
import Html.Attributes exposing ( .. ) | |
import Html.Events exposing ( onClick ) | |
import Window | |
import Task | |
main = Html.program | |
{ init = ( ( initSize, initSize ), triggerGetSize ) | |
, view = view | |
, update = update | |
, subscriptions = subscriptions | |
} | |
initSize = { width = 10, height = 10 } | |
triggerGetSize = Task.perform ( \_ -> None ) ( \x -> SetSize2 x ) Window.size | |
type Msg = SetSize1 Window.Size | SetSize2 Window.Size | GetSize | None | |
type alias Model = ( Window.Size, Window.Size ) | |
view: Model -> Html Msg | |
view model = | |
let viewStyle = | |
style | |
[ ( "padding", "20px" ) | |
, ( "background", "lightgrey" ) | |
, ( "display", "block" ) | |
] | |
( size1, size2 ) = model | |
in | |
div | |
[ onClick GetSize ] | |
[ div [ viewStyle ] [ text ( toString size1 ) ] | |
, div [ viewStyle ] [ text ( toString size2 ) ] ] | |
update: Msg -> Model -> ( Model, Cmd Msg ) | |
update msg model = | |
let ( size1, size2 ) = model | |
in | |
case msg of | |
SetSize1 size -> ( ( size, size2 ), Cmd.none ) | |
SetSize2 size -> ( ( size1, size ), Cmd.none ) | |
GetSize -> ( model, triggerGetSize ) | |
None -> ( model, Cmd.none ) | |
subscriptions model = | |
Sub.batch [ Window.resizes ( \size -> SetSize1 size ) ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment