简体   繁体   English

JsPlumb:如何动态创建新连接

[英]JsPlumb: How to create new connection dynamicaly

Here is a problem i've experienced with. 这是我遇到的一个问题。

    for (var i=0; i < arr.length; i++) {
        var instance = initializeInstance(wrapperID);
        allConnections.push(instance.connect ( 
            { 
                source  : connectionsArray[j].source, 
                target  : connectionsArray[j].target
            }
        ));
        jsPlumb.fire(".some-selector", instance);
    }

So this is the way i'm saving all the connections on the page between the elements. 因此,这就是我在元素之间的页面上保存所有连接的方式。 User has the possibility to add dynamically new elements to the page ... he/she is doing it by dragging the mouse pointer from one element to another on the page ..... i dunno how to add newly created connection into my allConnections array. 用户可以在页面上动态添加新元素...他/她正在通过将鼠标指针从页面上的一个元素拖到另一个元素来实现它.....我不知道如何将新创建的连接添加到我的allConnections中数组。

   newInstance = initializeInstance(wrapperID);
   newInstance.bind('connection', function(info){
       //   TO DO: push to allConnections Array newly created connection
       //   allConnections.push(this.connect(info.sourceId, info.targetId)); - this line does'n work :(
   });

How to add newly created connection on UI to my allConnections array ????? 如何将UI上新创建的连接添加到我的allConnections数组中?

Why do you need your own array ?! 为什么需要自己的阵列? You can always query jsPlumb to return all connections. 您始终可以query jsPlumb以返回所有连接。

jsPlumb.getAllConnections() or jsPlumb.select() jsPlumb.getAllConnections()jsPlumb.select()

Above will return a connection array. 上面将返回一个连接数组。 You can use it everywhere instead of using allConnections (array from the code in question) 您可以在任何地方使用它,而不必使用allConnections(相关代码中的数组)

Docs - http://jsplumbtoolkit.com/doc/querying 文件-http: //jsplumbtoolkit.com/doc/querying

Secondly, if you still want to add it to your array, then you can bind to events provided by jsPlumb. 其次,如果您仍想将其添加到数组中,则可以绑定到jsPlumb提供的事件。 connection event is fired whenever a new connection is created. 每当创建新连接时,都会触发connection事件。

Docs - http://jsplumbtoolkit.com/doc/events#evt-connection 文件-http: //jsplumbtoolkit.com/doc/events#evt-connection

Thirdly, there is no need to create multiple instances of jsPlumb. 第三,不需要创建jsPlumb的多个实例。 You can simply use jsPlumb.connect or create a single instance of it & use it everywhere. 您可以简单地使用jsPlumb.connect或创建它的单个实例并在任何地方使用它。 Currently, the code uses jsPlumb , instance and newInstance and the last two being created every now & then. 当前,代码使用jsPlumbinstancenewInstance并且现在和之后都创建最后两个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM