Skip to content

Instantly share code, notes, and snippets.

@danyx23
Last active January 26, 2016 05:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danyx23/23ab6572e7292e66e5ae to your computer and use it in GitHub Desktop.
Save danyx23/23ab6572e7292e66e5ae to your computer and use it in GitHub Desktop.
module PortsExample where
import Html exposing (div, button, text)
import Html.Events exposing (onClick)
import Signal exposing (Mailbox, mailbox, send)
import StartApp
import Effects
type alias Model = String
init : (Model, Effects.Effects a)
init = ("initial string", Effects.none)
type Action
= SetURL String
| ResetURL
view : Signal.Address Action -> Model -> Html.Html
view address model =
div []
[ div [] [ text model ]
, button [ onClick address (SetURL "Hello from the Elm button") ] [ text "SET" ]
]
update : Action -> Model -> (Model, Effects.Effects a)
update action model =
case action of
SetURL url ->
(url, Effects.none)
ResetURL ->
("Reset", Effects.none)
port getURL : Signal (String)
setUrlFromJsSignal : Signal Action
setUrlFromJsSignal = Signal.map SetURL getURL
app =
StartApp.start { init = init, view = view, update = update, inputs = [ setUrlFromJsSignal ]}
main = app.html
<!-- excerpt -->
<script>
// get an empty <div>
var div = document.getElementById('root');
var button = document.getElementById('someButton');
// embed our Elm program in that <div>
var elmInstance = Elm.embed(Elm.PortsExample, div, {
getURL: 'URL not set'
});
button.onclick = function() {
elmInstance.ports.getURL.send("hi from JS");
event.preventDefault();
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment