你可以如何在Fable中创建一个带有详细信息对象的CustomEvent?

huangapple go评论52阅读模式
英文:

How can I create a CustomEvent with detail object in Fable?

问题

在JavaScript中,可以创建一个CustomEvent,并在分发事件时包含一个包含详细数据的第二个参数。例如:

let evt = new CustomEvent("hello", {
    detail: { name: "John" }
});
elem.dispatchEvent(evt);

使用Fable,可以创建一个没有详细数据的CustomEvent,如下所示:

let evt = CustomEvent.Create "hello"
elem.dispatchEvent evt

Browser.Events中的定义如下:

type [<AllowNullLiteral>] CustomEventType =
    [<Emit("new $0($1...)")>] abstract Create : typeArg: string * ?eventInitDict: CustomEventInit -> CustomEvent
    [<Emit("new $0($1...)")>] abstract Create : typeArg: string * ?eventInitDict: CustomEventInit<'T> -> CustomEvent<'T>

但是我无法弄清楚如何创建CustomEventInit,因为我认为这是一个接口。

let details:CustomEventInit = { detail = {name = "John"}} //ERROR: This type is not a record type
let evt = CustomEvent.Create ("hello", details)
elem.dispatchEvent evt

希望对如何实现这一点有所帮助!

英文:

In Javascript it is possible to create a CustomEvent and include a second argument containing detail data to pass when the event is dispatched. For example:

let evt = new CustomEvent(&quot;hello&quot;, {
    detail: { name: &quot;John&quot; }
  });
elem.dispatchEvent(evt);

With Fable I can create a CustomEvent without the detail data as follows:

let evt = CustomEvent.Create &quot;hello&quot;
elem.dispatchEvent evt

The definition in Browser.Events is as follows:

type [&lt;AllowNullLiteral&gt;] CustomEventType =
    [&lt;Emit(&quot;new $0($1...)&quot;)&gt;] abstract Create : typeArg: string * ?eventInitDict: CustomEventInit -&gt; CustomEvent
    [&lt;Emit(&quot;new $0($1...)&quot;)&gt;] abstract Create : typeArg: string * ?eventInitDict: CustomEventInit&lt;&#39;T&gt; -&gt; CustomEvent&lt;&#39;T&gt;

But I can't work out how to create a CustomEventInit as I think this is an interface.

let details:CustomEventInit = { detail = {name = &quot;John&quot;}} //ERROR: This type is not a record type
let evt = CustomEvent.Create (&quot;hello&quot;, details)
elem.dispatchEvent evt

Any help on how to achieve this would be greatly appreciated!

答案1

得分: 1

CustomEventInit 是一个类类型,因此您可以使用对象表达式来实例化它:

let details =
    let mutable bubbles = true
    let mutable cancelable = true
    let mutable composed = true
    let mutable detail : obj = "John"
    {
        new CustomEventInit with
            member _.bubbles
                with set(value) = bubbles <- value
                and get() = bubbles
            member _.cancelable
                with set(value) = cancelable <- value
                and get() = cancelable
            member _.composed
                with set(value) = composed <- value
                and get() = composed
            member _.detail
                with set(value) = detail <- value
                and get() = detail
    }

或者,如果您喜欢,您可以创建一个命名的子类。

英文:

CustomEventInit is a class type, so you can instantiate it using an object expression:

let details =
    let mutable bubbles = true
    let mutable cancelable = true
    let mutable composed = true
    let mutable detail : obj = &quot;John&quot;
    {
        new CustomEventInit with
            member _.bubbles
                with set(value) = bubbles &lt;- value
                and get() = bubbles
            member _.cancelable
                with set(value) = cancelable &lt;- value
                and get() = cancelable
            member _.composed
                with set(value) = bubbles &lt;- value
                and get() = bubbles
            member _.detail
                with set(value) = detail &lt;- value
                and get() = detail
    }

Or you could create a named subclass if you prefer.

huangapple
  • 本文由 发表于 2023年2月16日 08:24:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466698.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定