问题
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?
回答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?
来源:https://stackoverflow.com/questions/21113119/cy-load-elements-are-lost