Closed maxkfranz closed 9 years ago
Too many complaints and too much confusion over this function.
If the old data has positions defined already, cy.add()
should be the same for your usecase.
I'll put this off to 2.4, and it will be backwards compatible -- but I don't think anyone new should be using cy.load()
.
Perhaps there's a better way to do this, but what I am doing is resetting the core to have 0 data (but to maintain the styles and any additional click handlers I've set up). I don't want to wipe the core, but rather zero it out so that I can call cy.add()
and have things work nicely.
I've extended the core with a 'reset' function that internally calls load()
with an empty collection. The callback allows me to pass in a function that can be called after load()
has finished.
cytoscape('core', 'reset', function( callback ){
var cy = this;
cy.load(cy.collection(), null, callback);
return this; // chainability
});
I use this function later when loading in a saved data set:
var savedNetworkData = network.get('data');
// need a detached copy of the elements so changes to the data in the graph
// don't affect the model
var elements = $.extend(true, {}, savedNetworkData.elements);
// clears the graph
this._cy.reset(_.bind(function() {
this._cy.startBatch();
this._cy.add(elements);
this._cy.endBatch();
var opts = {
name: 'preset',
};
this._cy.layout(opts);
}, this));
I think a call to cy.elements().remove()
could replace your cy.reset()
call. Because there's no async layout, you don't need the callback or you could just have:
cytoscape('core', 'reset', function( callback ){
var cy = this;
cy.elements().remove();
callback();
return this; // chainability
});
I do a similar thing as bertomaniac.
Using add
or remove
and layout
after them tended to mess with node positions, so every time I add/remove nodes I call
cy.layout(layout);
cy.load( cy.elements('*').jsons(), function() { cy.center(node)} );
And it does the trick.
So if we set up cyto but have the data later on, should we used the "add" and then run a layout?
@MissChocoe Yes. You can always use that manual workflow, even at init if you want (i.e. start with empty graph in init options).
Any reason for deprecation? I currently use this as a "reset" of sorts so that I can persist existing data and load old, saved sets.