Issue #2319Opened October 8, 2019by glassdimly0 reactions

How to Force Component Re-Render?

Question

const component = DomComponents.addType(name, {
  model: {
      removed() {
          const pathwaysParent = this.parent().closest('interactive-pathways');
          if (pathwaysParent) {
           // This doesn't work. There is no such function. I got this example from: https://github.com/artf/grapesjs/issues/1227#issuecomment-399754486
            pathwaysParent.render();
          }
        },
  }
}

When I try the following in place of pathwaysParent.render(), it obviously creates an infinite loop:

           const coll = pathwaysParent.collection;
            const at = coll.indexOf(pathwaysParent);
            coll.remove(pathwaysParent);
            coll.add(pathwaysParent, { at });

I can access the el at pathwaysParent.view.el, but I'm still looking for a good way to force the DOM component to re-render in a non-hacky way.

Answers (1)

glassdimlyOctober 9, 20190 reactions

I ended up firing an event on each object removed that caused them to trigger their own updates. I did this with a config object, but here's a more specific draft of my code. These are shadow dom components so this.parent().closest('pathway') is a selector for <pathway></pathway>

          // this.closest(whatever) is always 0.
          const pathway = this.parent().closest('pathway');
          // This is only dispatched once, because (likely) nodes are removed from parent -> down.
            if (pathway) {
              pathway.view.el.dispatchEvent(
                new CustomEvent(`step:title-updated`, {
                  bubbles: true,
                }),
              );
            }

In general, re-rendering is not the editor's job. Though I would have been content refreshing all the content, but no access to the editor object from within removed.

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.