[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)
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
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.
Issue #1815
[Bug]: component:mount is fired multiple times
I had been using editor events and I noticed that component:mount event fires multiple times when a component is dragged from blocks to can...
Issue #1895
[QUESTION] Drag component over canvas with script, script appended multiple times.
Hi, not sure if this is a bug or not. But I noticed a weird behavior, when I drag a block that has a component that contains a script funct...
Issue #1727
[QUESTION] Event to get children view
I have a problem with a component. When I drop a component into the canvas I create a simple object. Before appending this object I need to...
Issue #1951
[QUESTIONS] change or add event on add component from block
is it possible ?, change the drag and drop event to on click when adding a new component, I want to add components by clicking the block th...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.