jsPlumb如何删除重复的连接

时间:2014-06-11 04:54:43

标签: javascript jquery jsplumb

我正在尝试使用jsPlumb时避免重复连接(具有相同源和目标的2个连接)。有没有办法做到这一点,而无需修改jsPlumb.js本身?

http://jsfiddle.net/uQdfq/
(从task1拖到task3 两次

我不希望在(1)中添加特定端点的限制。

我的.task在被调用时被定义为可能的目标/来源 - 也就是整个div可以是源/目标,而不仅仅是某个端点:

  addTask($('#project1'), 'task' + 1);

功能本身:

// Adds a task div to the specific project
function addTask(parentId, id) {
  var newState = $('<div>').attr('id', id).addClass('task')

  // A title for the task
  var title = $('<div>').addClass('title').text(id);
  newState.append(title);

  $(parentId).append(newState);

  // Makes the task div a possible target (i.e. connection can be dragged to)
  jsPlumb.makeTarget(newState, {
    anchor: 'Continuous'
  });

  // Makes the task div a possible source (i.e. connection can be dragged from)
  jsPlumb.makeSource(newState, {
    anchor: 'Continuous'
  });
}

添加一些阻止创建重复连接的可能性的最佳方法是什么。

1 个答案:

答案 0 :(得分:4)

jsPlumb.bind('connection',function(info){
 var con=info.connection;
 var arr=jsPlumb.select({source:con.sourceId,target:con.targetId});
 if(arr.length>1){
    jsPlumb.detach(con);
 }
});

每当创建新连接时,请检查是否存在具有相同来源的连接。目标,如果是,则分离新的。