Hello,
For a particular application I needed to listen for scroll events on the main document. To do this I searched this forum and found the following post: Elm-Scroll - An Effect Manager for Scroll Events
In a comment, user ilias suggests a ports based solution, and even demonstrated it using ellie: https://ellie-app.com/qqp6pVJRQa1/0
A slight variation of this worked perfectly for what I was attempting to do. But in that comment ilias suggests that in the next release there would be a pair of functions onWindow
and onDocument
to subscribe to events from the window and the document respectively.
So my question is first of all, am I correct that this pair of functions onWindow
and onDocument
never made it into the 0.19 release?
Secondly, assuming that to be the case, is the approach suggested by the linked ellie instance (https://ellie-app.com/qqp6pVJRQa1/0) roughly the correct one?
To save you visiting the link, the approach briefly (for a ‘wheel’ event) is:
port onWheel : (Value -> msg) -> Sub msg
wheelDecoder : Decoder Model
wheelDecoder =
Decode.map2 Model
(Decode.field "deltaX" Decode.float)
(Decode.field "deltaY" Decode.float)
subscriptions : model -> Sub Msg
subscriptions _ =
onWheel
(\val ->
case Decode.decodeValue wheelDecoder val of
Ok model ->
Wheel model
Err _ ->
NoOp
)
Then in your index.html (or index.js):
var app = Elm.Main.init()
document.addEventListener("wheel", app.ports.onWheel.send);