Skip to content

Instantly share code, notes, and snippets.

@jesperp
Created July 23, 2016 18:29
Show Gist options
  • Save jesperp/ecbcfd77bf729cef800d1df2a6d11438 to your computer and use it in GitHub Desktop.
Save jesperp/ecbcfd77bf729cef800d1df2a6d11438 to your computer and use it in GitHub Desktop.
import Html
import Html.App
import Html.Attributes exposing (..)
import Html.Events exposing (onClick)
import Navigation exposing (Location)
import RouteUrl
import RouteUrl exposing (UrlChange, HistoryEntry(NewEntry))
import String
type alias Model = Int
type Msg = Increment | Decrement | Reset | Set Int
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
Increment -> (model + 1, Cmd.none)
Decrement -> (model - 1, Cmd.none)
Set value -> (value, Cmd.none)
Reset -> (0, Cmd.none)
view : Model -> Html.Html Msg
view model =
Html.div
[]
[ Html.h1 [] [Html.text <| toString model]
, Html.br [] []
, Html.button [onClick Increment] [Html.text "Increment"]
, Html.button [onClick Decrement] [Html.text "Decrement"]
, Html.button [onClick Reset] [Html.text "Reset"]
]
delta2url : Model -> Model -> Maybe UrlChange
delta2url previous current =
Just (UrlChange NewEntry ("#" ++ toString current))
location2messages : Location -> List Msg
location2messages location =
case String.toInt <| String.dropLeft 1 location.hash of
Ok count ->
[Set count]
_ ->
[]
main : Program Never
main =
RouteUrl.program
{ init = (0, Cmd.none)
, subscriptions = (\_ -> Sub.none)
, update = update
, view = view
, delta2url = delta2url
, location2messages = location2messages
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment