Reputation: 37
I'm using different collections of edges which are switched in CY based on user interactions so in every moment only one of these collections is in CY instance. I need to create new elements in one of this "remote" collections directly from JS object data and I can't figure how to do it other than create new edges with cy.add(eleObjs) and then remove these elements from CY.
Is there a way how to create CY elements from JS object (eleObj) other than with cy.add(eleObj)? I tried eles.add(eleObj) but it doesn't work.
I thought about creating a separate CY instance only for this purpose but I don't know if it's the best solution and one of the problems is that in this case I would also need to synchronize the nodes between CY instances in order to be able to create new edges.
Upvotes: 1
Views: 571
Reputation: 12242
You can think of having a removed node as being analogous to a file being in the trash in your filesystem. You wouldn't create removed nodes, just as you wouldn't create new files in the trash.
Add your elements to the graph, as you normally would. You can set a class with display: none
on the elements you don't want displayed. You can also perform layouts on only the subset of the graph that's visible via eles.layout()
.
Explanation: Having detached elements that are not yet associated with a graph is inconsistent with the model. And creating elements without an associated graph wouldn't buy you any performance gains -- as the elements wouldn't hold anything more than the JSON you already had. And it wouldn't buy you any convenience -- as you couldn't do anything with the elements and cy.add( elesJson )
is just as convenient as cy.add( preexistingEles )
.
Upvotes: 1