0

我使用默认链接,我想限制链接的源和目标,因为我只想要 和 之间的rect(source)链接circle(target)

我已经尝试过了link.on(change:source)link.on(change:target)但是当我想要的时候,这个事件并没有启动。

有人知道这个问题的一些解决方案吗?

    var defaultLinks =  new joint.dia.Link({
            attrs: {
                '.marker-source': {transform: 'scale(0.001)' },
                '.marker-target': {fill:'black', d: 'M 10 0 L 0 5 L 10 10 z' },
                '.connection-wrap': {
                    stroke: 'black'
                }
            },
            smooth:true,
            path: []
    });
    defaultLinks.on('change:source',function(){
        alert("change source")
    });
    defaultLinks.on('change:target',function(){
        alert("change source")
    });

    this.paper = new joint.dia.Paper({
        el: this.paperScroller.el,
        width: 1200,
        height: 1000,
        gridSize: 10,
        perpendicularLinks: true,
        model: this.graph,
        defaultLink: defaultLinks
    });
4

1 回答 1

4

您可以validateConnection(cellViewS, magnetS, cellViewT, magnetT, end, linkView)在 Paper 选项 ( http://jointjs.com/api#joint.dia.Paper ) 中使用该功能。本教程展示了用法:http: //jointjs.com/tutorial/ports#restrict。在您的情况下,您将使用类似(未测试)的东西:

var paper = new joint.dia.Paper({

    validateConnection: function(cellViewS, magnetS, cellViewT, magnetT, end, linkView) {
        // Only rectangle can be the source, if it is not, we do not allow such connection:
        if (cellViewS.model.get('type') !== 'basic.Rect') return false;
        // Only circle can be the target, if it is not, we do not allow such a connection:
        if (cellViewT.model.get('type') !== 'basic.Circle') return false;
        // Connection is allowed otherwise.
        return true;
    },
    ...
})
于 2014-06-05T19:51:48.620 回答