从 JointJS 图中按 ID 删除 link?

Remove link by ID from JointJS graph?

JointJS 提供了从图形中删除 links 的方法,例如 dia.Link.prototype.disconnect and dia.Link.prototype.remove

但是,它们首先取决于能否访问 link 对象。有什么方法可以通过 ID 查询 link 对象的 JointJS 图 (joint.dia.Graph)?

我可以手动维护一个从 ID 到 link 对象的 JS 映射,但这听起来很乏味。

graph.getCell(linkId) 不符合您的要求吗?

例如

graph.removeCells(graph.getCell(linkId))

如果您想访问任何 link,您有两个选择,然后您可以删除它们

_.each(cellView.paper.model.getLinks(), function(link) {
        console.log(link.id, link.get('source'), link.get('target'))
     })

OR

_.each(cellView.paper.model.get('cells'), function(cell) {
    if (cell instanceof joint.dia.Link) {
       // cell is a link
        console.log(cell.id, cell.get('source'), cell.get('target'))
    } else {
        // cell is an element
        console.log(cell.id, cell.get('position'), cell.get('size'), cell.get('angle'))
   }
})

由 David Durman 本人提供 https://groups.google.com/forum/#!topic/jointjs/cWJAK9gSC-Q

在其他图表上你可以发出一个事件

graph.on('change:source change:target', function(link) {
you can use link.remove()
}