|
import Effects exposing (Never, Effects) |
|
import StartApp |
|
import Task |
|
import Html exposing (..) |
|
import Html.Events exposing (onClick) |
|
|
|
type alias Model = |
|
{ text : String |
|
, number : Int |
|
, jsAddress : Signal.Address JSAction} |
|
|
|
init : Signal.Address JSAction -> (Model, Effects.Effects Action) |
|
init address = ({text="Nothing", number=0, jsAddress=address}, Effects.none) |
|
|
|
update action model = |
|
case action of |
|
TextFromJS str -> ({model|text = str}, Effects.none) |
|
ToJS -> ({model|number = model.number+1}, toJSEffect model.jsAddress model.number) |
|
NoOp () -> (model, Effects.none) |
|
|
|
type Action = TextFromJS String | ToJS | NoOp () |
|
|
|
view : Signal.Address Action -> Model -> Html |
|
view address model = |
|
div [] |
|
[ button [onClick address ToJS] [text "Increment and send old value to JS"] |
|
, br [] [] |
|
, text <| "From JS: " ++ model.text |
|
, br [] [] |
|
, text <| "Current Value: " ++ (toString model.number) |
|
, br [] [] |
|
, button [onClick model.jsAddress (Number model.number)] [text "Send current value to JS"] |
|
, br [] [] |
|
, button [onClick model.jsAddress (Text "Hello from Elm")] [text "Send greeting to JS"] |
|
] |
|
|
|
toJSEffect address no = |
|
Signal.send address (Number no) |
|
|> Task.map NoOp |
|
|> Effects.task |
|
|
|
app = |
|
StartApp.start |
|
{ init = init toJsMailbox.address |
|
, update = update |
|
, view = view |
|
, inputs = [Signal.map TextFromJS fromJS] |
|
} |
|
|
|
main : Signal Html.Html |
|
main = |
|
app.html |
|
|
|
port tasks : Signal (Task.Task Never ()) |
|
port tasks = app.tasks |
|
|
|
|
|
|
|
type JSAction = Number Int | Text String | NoJSOp |
|
|
|
toJsMailbox = Signal.mailbox NoJSOp |
|
|
|
intFilter : JSAction -> Maybe Int |
|
intFilter action = |
|
case action of |
|
Number n -> Just n |
|
Text s -> Nothing |
|
NoJSOp -> Nothing |
|
|
|
stringFilter : JSAction -> Maybe String |
|
stringFilter action = |
|
case action of |
|
Number n -> Nothing |
|
Text s -> Just s |
|
NoJSOp -> Nothing |
|
|
|
port intToJS : Signal Int |
|
port intToJS = Signal.filterMap intFilter 0 toJsMailbox.signal |
|
|
|
port stringToJS : Signal String |
|
port stringToJS = Signal.filterMap stringFilter "Default" toJsMailbox.signal |
|
|
|
|
|
port fromJS : Signal String |