I need to measure part of my view (using Browser.Dom.getElement). This can, of course, only happen once the view is rendered. However, the custom ‘on’ function of the html event system is (apparently) not working for onLoad
:
view : Model -> Html Msg
view model =
Html.div [Html.Events.on "load" (Decode.succeed DoOnLoadStuff)]
[...]
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
...
DoOnLoadStuff ->
Debug.log (Debug.toString "In DoOnLoadStuff")
( model, getTextSize )
In update, the code following DoOnLoadStuff
never runs (and there is no log to the console).
However, when I replace "load"
with "click"
, a click does send the message, and the update code is run.
onLoad
is clearly ‘non-passive’ by a naive definition, since it fires without user interaction, but I don’t think it satisfies ‘non-passive’ by the Passive event listeners definition.
There is an issue posted about this specifically for elm-ui and images:
Custom onLoad event handler does not work for images #14
but the problem seems to be deeper than that.
My questions:
-
Am I doing something wrong?
-
Is there an Elm native way to ‘do something when the view is ready’ ?
-
More broadly, what options are there to make stuff happen without user interaction?
All packages in the test system are latest at time of writing:
"elm-version": "0.19.0",
"dependencies": {
"direct": {
"elm/browser": "1.0.1",
"elm/core": "1.0.2",
"elm/html": "1.0.0",
"elm/json": "1.1.3",
"mdgriffith/elm-ui": "1.1.0"
},
"indirect": {
"elm/time": "1.0.0",
"elm/url": "1.0.0",
"elm/virtual-dom": "1.0.2"
}
},