我想做类似的事情:
open Webapi.Dom;
let addListener = (element) =>
EventTarget.addEventListener("click", onSubscribeClick, EventTarget.asEventTarget(element));
let addOrRemoveListeners = (handler, ()) => {
let elements = Document.querySelectorAll({j|[$subscriptionIdAttr]|j}, document);
Js.Array.forEach(handler, NodeListRe.toArray(elements) |> Js.Array.map(Element.ofNode))
};
let addListeners = addOrRemoveListeners(addListener);
let removeListeners = addOrRemoveListeners(removeListener);
但我得到一个错误addOrRemoveListeners(addListener);
:
This is:
(Dom.eventTarget) => unit
But somewhere wanted:
(Dom.node) => unit
我应该如何从 转换Dom.eventTarget
为Dom.node
?
编辑:我最终得到了这个:
let addListener = Element.addEventListener("click", onSubscribeClick);
let removeListener = Element.removeEventListener("click", onSubscribeClick);
let addOrRemoveListeners = (handler, ()) => {
let elements = Document.querySelectorAll({j|[$(subscriptionIdAttr)]|j}, document);
let () =
elements
|> NodeListRe.toArray
|> Array.map(Element.ofNode)
|> Array.to_list
|> List.filter(Js.Option.isSome)
|> List.map(Js.Option.getExn)
|> List.iter(handler);
()
};
let addListeners = addOrRemoveListeners(addListener);
let removeListeners = addOrRemoveListeners(removeListener);