Skip to content

Instantly share code, notes, and snippets.

@napcs
Created April 8, 2017 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save napcs/39694c540cf608517e08fc35f1685729 to your computer and use it in GitHub Desktop.
Save napcs/39694c540cf608517e08fc35f1685729 to your computer and use it in GitHub Desktop.
Elm with Flags
module Main exposing (..)
import Html exposing (Html, div, p, text)
-- Data type for the flags
type alias Flags =
{ user : String
, token : String
}
-- Data type for the model that represents the state of the application.
type alias Model =
{ user: String
, token :String
, message: String
}
-- Message data type - NoOp cos this app doesn't do anything.
type Msg = NoOp
-- Pull the flags in, create new model.
init : Flags -> ( Model, Cmd Msg )
init flags =
( (Model flags.user flags.token ""), Cmd.none )
-- Handle updates. Don't really have anything to do, so just return the model and Cmd.none as tuple.
update : Msg -> Model -> (Model, Cmd Msg)
update msg model=
( model, Cmd.none)
-- every app needs a main. Kick off the process.
main =
-- programWithFlags requires init, update, subscriptions, and view.
-- Just do an anonymous function for subs since we don't need any.
-- \_ -> means anonymous function that needs no args.
Html.programWithFlags
{ init = init
, update = update
, subscriptions = \_ -> Sub.none
, view=view }
-- display the things in the model
view: Model -> Html msg
view model =
div []
[ p [] [text model.user]
, p [] [text model.token]
]
<html>
<head>
<title>Elm app</title>
</head>
<body>
<script>
// the valueso of the object get sent to the Elm app.
var app = Elm.Main.fullscreen( {user: "Brian", token: "abcd"} );
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment