当我在链接上分配像源/目标这样的单元格时的事件?

时间:2014-06-02 11:25:35

标签: javascript jointjs

我使用默认链接,我想限制来自链接的来源和目标,因为我只想要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
    });

1 个答案:

答案 0 :(得分:4)

您可以使用纸张选项(http://jointjs.com/api#joint.dia.Paper)中的validateConnection(cellViewS, magnetS, cellViewT, magnetT, end, linkView)功能。本教程介绍了用法: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;
    },
    ...
})