Created
November 24, 2015 18:28
-
-
Save larryweya/89ff04103427cc592196 to your computer and use it in GitHub Desktop.
Publish state to a port
This file contains 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
module Interop where | |
import Signal | |
import String | |
import Html exposing (..) | |
import Html.Attributes exposing (value) | |
import Html.Events exposing (on, targetValue) | |
type Field = Name | |
| Age | |
type Action = NoOp | |
| Change Field String | |
type alias Model = { | |
name : String, | |
age : Int | |
} | |
main = | |
Signal.map (view actions.address) model | |
initialModel : Model | |
initialModel = | |
{ | |
name = "", | |
age = 0 | |
} | |
update : Action -> Model -> Model | |
update action model = | |
case action of | |
NoOp -> | |
model | |
Change field value -> | |
case field of | |
Name -> | |
{ model | name = value } | |
Age -> | |
{ model | age = Maybe.withDefault 0 <| Result.toMaybe <| String.toInt value } | |
view : Signal.Address Action -> Model -> Html | |
view address model = | |
div | |
[] | |
[ div | |
[] | |
[ label [] [text "Name"] | |
, input | |
[ value model.name | |
, on "change" targetValue (\value -> Signal.message address (Change Name value)) | |
] | |
[] | |
] | |
, div | |
[] | |
[ label [] [text "Age"] | |
, input | |
[ value <| toString model.age | |
, on "change" targetValue (\value -> Signal.message address (Change Age value)) | |
] | |
[] | |
] | |
] | |
actions : Signal.Mailbox Action | |
actions = | |
Signal.mailbox NoOp | |
model : Signal Model | |
model = | |
Signal.foldp update initialModel actions.signal | |
port save : Signal (Maybe Int) | |
port onSave : Signal Model | |
port onSave = | |
Signal.map (\_ -> initialModel) save |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment