1

I'm trying to store the current graph in browser on the server side and now I'm testing how to. I ran into a strange Problem: after jsoning, decycling, stringifying and reloading the graph, the elements are somehow lost.

var j = network_tab.cy.json();
j = JSON.decycle(j);
j = JSON.stringify(j);
network_tab.cy.load(j);

Before decycling the graph, I cannot stringify it. If you look in the stringified version, you can see, that the elements are still there, but after loading, they are gone.

Maybe I have a problem in general with the cy.load() function, but I do hope you guys can help me.

EDIT: The problem seems to be with the stringify function. Then my question boils down how to make the whole object readable for load().

 An element must be of type `nodes` or `edges`; you specified `undefined`   
 console.error( msg );
 cytoscape.js (line 244)
 TypeError: obj._private is undefined

I want to copy the whole thing, but especially the elements, the positions. So is it better to reinitialize the graph completely and when so, how do I add the positions of the nodes?

Sileadim
  • 11
  • 4

1 Answers1

1

(1) cy.json() is undocumented (and probably not ready for use) until 2.1

(2) You shouldn't need to use JSON.decycle() unless your own data that you're putting in elements have references to one another -- not a good idea in general.

(3) cy.json() gives you the whole graph state as used in intialisation -- not for cy.load(). You could pass json.elements to cy.load() but not the root object, json.

(4) You can alternatively get each individual element as JSON via (the tested and documented prior to 2.1) ele.json() and build your own object to pass to cy.load().

(5) Have you inspected the object you're passing to cy.load()? Can you post it here? You're not passing a JSON string to cy.load(), are you?

maxkfranz
  • 11,896
  • 1
  • 27
  • 36
  • I tried to parse the whole cy.json() object, but that obviously didn't work. Now I'm trying to parse cy.json().elements to load, and everything works, but the positions. So I probably have to to completely reinitialize the graph and there I can add the positions? – Sileadim Jan 15 '14 at 11:47
  • When calling `cy.load()`, remember that a layout is called along with it -- as opposed to `cy.add()` which uses the element-specified positions. So, you need to use a preset layout or similar, or you need to use `cy.add()`. Docs: http://cytoscape.github.io/cytoscape.js/ – maxkfranz Jan 17 '14 at 18:40