0

如何将自定义事件分派给集合中的所有自定义对象?

class Node{...}

a = new Node(...);
b = new Node(...);

var nodeSet = new Set(...);
let myEvent = new CustomEvent(...);

for (var node of nodeSet){
     node.shape.dispatchEvent(myEvent); 
}

Node.shape 是一个Konva形状

由于我是 Javascript 的初学者,请保持回复易于理解,谢谢。

4

1 回答 1

1

为了使您的自定义事件与Konva Node对象一起工作,Shape子类有一个on方法可以让您直接在形状上注册您的侦听器。

做这个:

class Node{...}

a = new Node(...);
b = new Node(...);
a.shape.on('Yo!', handler);
b.share.on('Yo!', handler);
  // all 4000 shapes

var nodeSet = new Set(...);
let myEvent = new CustomEvent('Yo!');

// If you only have one handler for every shape's treatment of CustomEvent
for (var node of nodeSet){
     node.shape.on('Yo!', handler);
}

for (var node of nodeSet){
     node.shape.dispatchEvent(myEvent); 
}

于 2021-02-16T01:47:59.140 回答