React-diagrams: Add listeners after deSerializeDiagram action

Created on 1 Jun 2019  路  2Comments  路  Source: projectstorm/react-diagrams

I know that the listeners can be added on the node while the new node is created for example like

node = new ButtonNodeModel();
node.addListener({
  selectionChanged: function(nodeData){
  _this.nodeData(nodeData);
}

But when I deSerializeDiagram after saving it and retrieving back, How can I add back the listeners on the node?

applyDiagram = (engine) => {
        let tempModel = new DiagramModel();
        let ts = localStorage.getItem('sample');
        tempModel.deSerializeDiagram(JSON.parse(ts), engine);
        engine.setDiagramModel(tempModel);
        const modelNodes = tempModel.getNodes();
        // How to add those listeners back on the node now? 馃槙 
        this.forceUpdate();
    };

Most helpful comment

Thanks, @JokerNN, Your hint of how to bind the nodes really helped me.

Here is how I did it

const nodeKeys = Object.keys(tempModel.getNodes());
let _this = this;
    nodeKeys.forEach(node => {
    let noder = tempModel.getNodes()[node];
        noder.addListener({
             selectionChanged: function(nodeData){
                     _this.nodeData(nodeData);
              }
        });
   })

Closing this issue.

All 2 comments

I guess you have to do it again just like

tempModel.getNodes().forEach(node => {
  node.addListener(...)
})

Thanks, @JokerNN, Your hint of how to bind the nodes really helped me.

Here is how I did it

const nodeKeys = Object.keys(tempModel.getNodes());
let _this = this;
    nodeKeys.forEach(node => {
    let noder = tempModel.getNodes()[node];
        noder.addListener({
             selectionChanged: function(nodeData){
                     _this.nodeData(nodeData);
              }
        });
   })

Closing this issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

t-gacema picture t-gacema  路  4Comments

abhijitnandy2011 picture abhijitnandy2011  路  3Comments

gugaevkirill picture gugaevkirill  路  3Comments

quangas picture quangas  路  3Comments

Nesterov-Konstantin picture Nesterov-Konstantin  路  4Comments