Search code examples
javascripteventsocamljs-of-ocaml

js_of_ocaml and event_listener


I have a div in js_of_ocaml which I want to assign an onmouseover event, like

deck_div##onmouseover <- (fun () -> (* do stuff *) )

However, the function type expected is

(Dom_html.divElement Js.t, Dom_html.mouseEvent Js.t) Dom_html.event_listener

How to construct this event listener? deck_div has no method addEventListener.


Solution

  • I was thinking about same question some months ago and AFAIR I have succeeded with Html.handler. Maybe this example will help you

    https://github.com/ocsigen/js_of_ocaml/blob/master/examples/hyperbolic/hypertree.ml#L276

    let handle_drag element move stop click =
      let fuzz = 4 in
      element##onmousedown <- Html.handler
        (fun ev ->
           let x0 = ev##clientX and y0 = ev##clientY in
           ....