如何对JointJS中的链接删除事件做出反应

时间:2019-04-30 12:34:20

标签: jointjs

我使用以下代码来控制链接上的删除功能。如果链接符合特定条件,如何拦截remove事件并阻止它?

// add a remove button on hovered link
this.paper.on("link:mouseenter", function(linkView) {
  let tools = [new joint.linkTools.Remove({ distance: 20 })];

  linkView.addTools(
    new joint.dia.ToolsView({
      name: "onhover",
      tools: tools
    })
  );
});

// remove button on hovered link
this.paper.on("link:mouseleave", function(linkView) {
  if (!linkView.hasTools("onhover")) return;
  linkView.removeTools();
});

1 个答案:

答案 0 :(得分:1)

使用传递给linkTools.Remove构造函数的action参数找到了答案。

// add a remove button on hovered link
this.paper.on("link:mouseenter", function(linkView) {
  let tools = [
    new joint.linkTools.Remove({
      distance: 20,
      action: function(evt) {
        // do stuff and remove link using
        this.model.remove({ ui: true, tool: this.cid });
      }
    })
  ];

  linkView.addTools(
    new joint.dia.ToolsView({
      name: "onhover",
      tools: tools
    })
  );
});