【问题标题】:jsPlumb how to remove duplicate connectionsjsPlumb如何删除重复连接
【发布时间】:2014-06-11 04:54:42
【问题描述】:

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

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

我不想有添加特定端点的限制,例如 (1)。

我的.tasks 在被调用时被定义为可能的目标/源 - 也就是说,整个 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'
  });
}

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

【问题讨论】:

标签: javascript jquery jsplumb


【解决方案1】:
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);
 }
});

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

【讨论】:

  • 谢谢。非常便利。我做了一点调整,这样它还可以检查当前源是否是当前目标的目标(所以基本上你将无法进行反向连接),即:var arr2 = jsPlumb.select({ source: con.targetId, target: con.sourceId }); 然后我只需要做if (arr.length + arr2.length &gt; 1)
  • 这不是正确的答案,因为 detach(con) 在某些情况下不会正确删除所有端点:jsplumb.org/doc/removing.html。更好的方法是在首先建立连接之前进行检查。看到这个:stackoverflow.com/questions/22959893/…
  • @user590849 OP 从不想删除端点,您的情况可能会有所不同。
  • 我收到这样的错误: jsPlumb: fire failed for event connection : TypeError: jsPlumb.detach is not a function。怎么办?
猜你喜欢
  • 2012-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-02
  • 2017-09-17
相关资源
最近更新 更多