In elm you can pass a flag to an elm application like so:
Html / JS
<div id="elm"></div>
<script>
var app = Elm.Main.init({
node: document.getElementById('elm'),
flags: Date.now()
});
</script>
The elm app then gets the parameters on init:
init : Int -> ( Model, Cmd Msg )
init currentTime =
...
I've been through the fable documentation and to me it's not clear how I can achieve the same thing.
I see there is an option Program.runWith to send parameters on the init function, but I can't find documentation and I can't see from the compiled javascript how I should call the main function from the html file.
With fable, I want to be able to do something like this in the html file, but not sure what "Program.Run.." would be:
<body>
<div id="elmish-app" class="elmish-app"></div>
<script src="bundle.js"></script>
<script>
Program.Run({time: Date.now()});
</script>
</body>
Thanks
when creating your Elmish
Programyou can use|> Program.runWithin order to pass it an initial argument.Note that
initialininitis a string because it receives the argument ofProgram.runWith.In order to expose the API, in your browser the
hackyway is to do something like that:So you are setting register a
startApplicationfunction in the global scopewindow.A cleaner solution is to use configure
webpackin order to make it generate a library and register it for you in thewindowscope.In your webpack.config.js your output node should look like that:
Then in your code use an interface to expose a JavaScript friendly API:
And now you can call it from JavaScript using
Program.api.Run("initial value")