We are currently using the following code to dynamically update the palette with new nodes. This approach used to work as expected in the older GoJS version (2.3.5):
However, after upgrading to GoJS version 3.0.20, the above code no longer replaces the existing nodes in the palette. Instead, it appends the new nodes to the end of the existing ones.
Our use case requires a complete replacement of the existing palette nodes with the new ones, not an append.
While this does replace the nodes, it also clears the templateMap, which we want to retain as it’s reused elsewhere and we prefer not to repopulate it every time.
Could you please suggest an alternative approach that allows us to fully replace the palette nodes while preserving the existing templateMap?
Check the value of paletteJSON to make sure it is an Array with exactly as many node data objects as you are expecting.
Setting the Diagram.model property of any kind of diagram, including a Palette, definitely does not modify any templates. I cannot explain that behavior in your app.
Yes, paletteJSON is an array. I’ve updated my code so that instead of modifying palette.model.nodeDataArray directly, I now create a new model, set its nodeDataArray to the new paletteJSON, and then assign this model to the palette diagram. This approach successfully replaces the nodes in the palette instead of appending them.
Code that appends the nodes:
// If the palette diagram is already rendered, update the palette model with the new paletteJSON.
if (palette) {
palette.model.startTransaction('updatePaletteJSON');
palette.model.nodeDataArray = paletteJSON;
palette.model.commitTransaction('updatePaletteJSON');
} else {
const paletteModel = new go.GraphLinksModel();
paletteModel.nodeKeyProperty = 'key';
paletteModel.nodeDataArray = paletteJSON;
paletteModel.nodeCategoryProperty = 'templateId';
// Main Palette model rendering
palette = new go.Palette(paletteNode, {
model: paletteModel,
});
}
Code that replaces the JSON correctly:
const paletteModel = new go.GraphLinksModel();
paletteModel.nodeKeyProperty = 'key';
paletteModel.nodeDataArray = paletteJSON;
paletteModel.nodeCategoryProperty = 'templateId';
// If the palette diagram is already rendered, update the palette model with the new paletteJSON.
if (palette) {
palette.model = paletteModel;
} else {
// Main Palette model rendering
palette = new go.Palette(paletteNode, {
model: paletteModel,
});
}
Can we please know why there is a difference in behavior when updating the model between these two approaches?
In the first case, modifying nodeDataArray directly seems to append the nodes, while in the second case, assigning a new model replaces them entirely. It would be helpful to understand what causes this difference under the hood.
Here’s a complete stand-alone example that demonstrates both approaches. You might want to reload the page before clicking the other button, although the correct behavior happens either way.
It does not exhibit the behavior your code does. Might there be some other differences in how you configured your diagram(s)?
However, this still appends the nodes instead of replacing them. The alternative method, where the GraphLinksModel is re-initialized, works as expected.
As far as I can tell, there isn’t much difference between our diagram configuration and the example above.