grapesjs-lory-slider icon indicating copy to clipboard operation
grapesjs-lory-slider copied to clipboard

Extra content when add components in onRender

Open longdoan7421 opened this issue 5 years ago • 0 comments

Hi @artf,

Currently, I have to add some small components dynamically after user changes trait (eg: rewind, infinity, ...). My solution was add those by using onRender() method in Slider component. but the issue is when the Slider component re-render, it has extra components.

Here is a simple demo (for simplicity, I reset all components then add new ones): grapesjs-lory-slider

And this is my change in view of Slider component:

view: defaultView.extend({
      init() {
        const props = [
          'rewind'
        ];
        const reactTo = props.map(prop => `change:${prop}`).join(' ');
        this.listenTo(this.model, reactTo, this.render);
        const comps = this.model.components();

        // Add a basic template if it's not yet initialized
        if (!comps.length) {
          comps.add(`<div data-gjs-type="${frameName}">
              <div data-gjs-type="${slidesName}">${config.slideEls}</div>
          </div>
          <span data-gjs-type="${prevName}">${config.prevEl}</span>
          <span data-gjs-type="${nextName}">${config.nextEl}</span>`);
        }
      },

      onRender() {
        console.log('onRender', index);
        if (index == 0) {
          // ignore first render
        } else {
          const comps = this.model.components();
          // comps.reset();
          comps.add(`<h1>Text ${index}</h1>`);
        }

        index++;
      }
})

I hope you have some solutions about this, thank you and happy holiday :D

longdoan7421 avatar Dec 27 '19 08:12 longdoan7421