0

Is it possible to have multiple cytoscape graph instances that are unrelated but share the same source elements?

Here is an example of what i'm trying to do : https://jsfiddle.net/fa8hbdnh/

    var elements = [
        { data: { id: 'n1'}, position: {x:100, y: 100}},
        { data: { id: 'n2'}, position: {x:150, y :150}},
        //--->Edges--->
        { data: {id: 'e1', source: 'n1', target: 'n2'}},
    ];

    var graph1 = cytoscape({
            headless: true,
            elements: elements
    });
    var graph2 = cytoscape({
        headless: true,
        elements: elements
    });

    graph1.elements()[0].data('foo',100);          // Only changing graph1...
    console.log(graph2.elements()[0].data('foo')); // ...however graph2 is also modified

(this requires the cytoscape library - http://js.cytoscape.org/)

As you can see, I change graph1 but graph 2 is also affected. Is there a way to save data elements on one instance without affecting other instances?

dsat
  • 127
  • 1
  • 8

1 Answers1

1

Cytoscape.js just takes in what you pass it. It doesn't make any assumptions about your JSON, and it doesn't copy data -- because that would slow down >=90% of usecases.

Either copy your JSON before you pass it in or pass collections from the first instance to subsequent instances. Collections are always copied from one instance to another, as noted in the docs, because that's the only way passing collections makes sense.

maxkfranz
  • 11,896
  • 1
  • 27
  • 36