Issue #828Opened February 1, 2018by maxtacco0 reactions

[QUESTION] Components 'add' event fires different number of times

Question

Hi, I've noticed that if I add the following code in my custom component the event fires differently depending on whether a new child component is drag and dropped from blocks vs canvas.

 ...  
  view: defaultView.extend({  
      init() {  
        this.listenTo(this.model.components(), 'add', this.onComponentAdded);  
      },
      
      onComponentAdded(comp) {
        console.log(comp);
      },
  })  
   ...

Only one event is fired when I drag and drop a child component from the blocks section. But, when I drag and drop from canvas I can see this event firing twice and the first time some 'div' component is passed.

I wonder if this relates to https://github.com/artf/grapesjs/pull/202 or not.

Also, I can handle this event the way I want but checking for the avoidStore option of the component:

     onComponentAdded(comp) {
        if (!comp.opt.avoidStore) {
          console.log(comp); // the child component I want
        }
      },

Please let me know if this is the desired behavior and it is ok to use the check as in the example above. Thank you.

Answers (2)

artfFebruary 6, 20180 reactions

Hi @maxtacco, unfortunately, this is due to how the Sorter works when moves components and honestly, at the moment, I can't see better workaround than your one

lock[bot]September 18, 20190 reactions

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

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.