d3-dispatch
调度是一种低级交互机制,它允许你注册命名回调,然后使用任意参数调用它们。各种 D3 交互组件,如 d3-drag,使用 dispatch 向监听器触发事件。可以将其视为 EventTarget,只不过每个监听器都有一个明确定义的名称,因此很容易移除或替换它们。
🌐 Dispatching is a low-level interaction mechanism that allows you to register named callbacks and then call them with arbitrary arguments. A variety of D3 interaction components, such as d3-drag, use dispatch to emit events to listeners. Think of this as EventTarget except every listener has a well-defined name so it’s easy to remove or replace them.
例如,要为 start 和 end 事件创建调度:
🌐 For example, to create a dispatch for start and end events:
const dispatch = d3.dispatch("start", "end");然后你可以使用 dispatch.on 为这些事件注册回调:
🌐 You can then register callbacks for these events using dispatch.on:
dispatch.on("start", callback1);
dispatch.on("start.foo", callback2);
dispatch.on("end", callback3);然后,你可以使用 dispatch.call 或 dispatch.apply 调用所有 start 回调:
🌐 Then, you can invoke all the start callbacks using dispatch.call or dispatch.apply:
dispatch.call("start");像 function.call 一样,你也可以指定 this 上下文和任何参数:
🌐 Like function.call, you may also specify the this context and any arguments:
dispatch.call("start", {about: "I am a context object"}, "I am an argument");dispatch(...类型)
🌐 dispatch(...types)
来源 · 为指定的事件类型创建一个新的调度。每个类型都是一个字符串,例如 "start" 或 "end"。
dispatch.on(typenames, callback)
来源 · 为指定的 typenames 添加、删除或获取 callback。如果指定了 callback 函数,它将注册到指定的(完全限定的)typenames。如果给定的 typenames 已经注册了回调,则在添加新回调之前会先移除现有回调。
指定的 typenames 是一个字符串,例如 start 或 end.foo。类型后面可选地跟一个点(.)(.)和一个名称;可选名称允许注册多个回调以接收相同类型的事件,例如 start.foo 和 start.bar。要指定多个 typenames,请用空格分隔 typenames,例如 start end 或 start.foo start.bar。
🌐 The specified typenames is a string, such as start or end.foo. The type may be optionally followed by a period (.) and a name; the optional name allows multiple callbacks to be registered to receive events of the same type, such as start.foo and start.bar. To specify multiple typenames, separate typenames with spaces, such as start end or start.foo start.bar.
要移除指定名称 foo 的所有回调,请使用 dispatch.on(".foo", null)。
🌐 To remove all callbacks for a given name foo, say dispatch.on(".foo", null).
如果未指定 callback,则返回指定 typenames 的当前回调(如果有)。如果指定了多个类型名,则返回第一个匹配的回调。
🌐 If callback is not specified, returns the current callback for the specified typenames, if any. If multiple typenames are specified, the first matching callback is returned.
dispatch.copy()
来源 · 返回此调度对象的副本。对该调度的更改不会影响返回的副本,反之亦然。
dispatch.call(type, that, ...arguments)
来源 · 类似 function.call,调用为指定 type 注册的每个回调,传递给回调指定的 ... 参数,并将 that 作为 this 上下文。有关更多信息,请参见 dispatch.apply。
dispatch.apply(type, that, arguments)
来源 · 像 function.apply 一样,为指定的 type 调用每个注册的回调,将指定的 arguments 传递给回调,并将 that 作为 this 上下文。例如,如果你想在处理原生 click 事件后分发你的 custom 回调,同时保留当前的 this 上下文和参数,你可以这样说:
selection.on("click", function() {
dispatch.apply("custom", this, arguments);
});你可以将任意参数传递给回调函数;最常见的做法是,你可能会创建一个表示事件的对象,或者传递当前数据项(d)和索引(i)。有关更多信息,请参见 function.call 和 function.apply。
🌐 You can pass whatever arguments you want to callbacks; most commonly, you might create an object that represents an event, or pass the current datum (d) and index (i). See function.call and function.apply for further information.