How to update nodeviews without reusing existing nodes

In my app, I have a NodeSpec with

attrs: {
                id: {default: 'id'},
                lang: {default: ''},
},

and a Nodeview

class View {
  constructor(node, view, getPos) {
    const dom = document.createElement('div')
    const label = document.createElement('label')
    label.innerText = mytext(node, myConfig)
    this.contentDOM = document.createElement('div')
    dom.appendChild(label)
    dom.appendChild(this.contentDOM)
    this.dom = dom
  }
}

Here mytext(node, myConfig) will get some text by the node’s id and lang from myConfig.

Everything works as expected at first.

Now myConfig updates (from network). Accordingly, the document is also modified by some transactions. Some nodes are deleted, some added. Both cases work fine (everything updates accordingly).

But for some nodes, the id and lang does not change. The label in the nodeview does not update as the result of mytext(node, myConfig) (since myConfig is updated).

I find in NodeViewDesc.prototype.updateChildren, it tries to reusing existing nodes, which might be the cause. I really do not want to introduce an extra attribute for the node, so I am wondering if it is possible to toggle off this reusing mechanism.

The view isn’t aware of the dependency of your node views on myConfig, so indeed, it won’t automatically redraw these. And of course it reuses existing nodes—otherwise it’d redraw the entire document on every change, which would be terrible. You’ll have to somehow arrange for the node views to be redrawn—one possibility would be to, when the metadata changes, add a node decoration to all nodes of this type, since changes in decoration will cause the node to be updated.

Ok, I will try it out. Thanks.

Howdy, I have a similar “issue” where the NodeView should redraw when the value of editable changes. It seems like something of a hack to wrap the views in decorations, any thoughts on how to achieve that?

There’s currently no other way—the editor treats node views as only depending on their node and decorations, so other changes don’t trigger updates.

It might be reasonable to add a way to explicitly iterate over the active node views, but that doesn’t exist yet and I don’t know when I’d get to working on that.

Gotcha, I understand you have to limit it somewhere – the editor changing between read-only and editable seems significant enough that it might be good to make NodeViews also dependent on that value, but not If I’m the only one that’s asked for it :slight_smile: