How can I implement a "page load event"

982 views
Skip to first unread message

Peter Damoc

unread,
Nov 10, 2015, 6:05:00 AM11/10/15
to Elm Discuss
Starting from Elm Architecture Tutorial example 8. 

How can I trigger the animation after the page loads? 

I've tried naively to add an "onLoad" handler that does the same thing as "onClick" but it doesn't seam to work. 


--
There is NO FATE, we are the creators.
blog: http://damoc.ro/

Ryan Rempel

unread,
Nov 10, 2015, 1:07:56 PM11/10/15
to Elm Discuss
If you want something to happen automatically when the page loads, then I think the idiomatic way within the usual Elm architecture would be to supply an `Effects` to the `init` that you supply to `StartApp.start` (or the conceptual equivalent, if you're not using `StartApp`).

That is, when you initialize your model, you ought to be able to also supply an initial action to execute -- in `StartApp`, that would be through the `init` that you supply to `StartApp.start`.

I think the reason your `onLoad` approach might not have worked is that it would normally be attached too late -- that is, it would normally be attached *after* the "load" event had already fired, so it wouldn't accomplish anything.

I'm actually doing some related work-in-progress here at the moment:


However, the use cases for what I'm doing there are a bit exotic (or possibly even non-existent) -- you'd have to be in a situation where there was a bunch of DOM to load that might not be finished loading when your Elm code runs -- which wouldn't normally be the case, since normally the Elm code is what generates most of the DOM. So, normally one would imagine that whatever non-Elm-generated DOM exists has already loaded by the time your Elm code runs. But, I suppose it might not be absolutely inevitable.

Peter Damoc

unread,
Nov 11, 2015, 1:26:56 AM11/11/15
to Elm Discuss
Thanks Ryan!

Now the solution is so obvious that I feel silly for not thinking about it. :)


--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages