how to listen the components change in a component.
Question
init() { this.listenTo(this.model,'change:components', this.handleComponentsChange); },
I develop a component, I try to listen the child components change, then I add above code to view init, but the function doesn't fired when add/remove component.
how I can listen the components change in a component view?
thanks
Answers (3)
I couldn't get this to work either. Error reading components() when setting the listenTo. The tag is empty (no contents inside) on init, so this makes sense.
never mind... i was doing this in the init of the model instead of the view.
init() {
const children = this.model.components();
this.listenTo(children, 'add remove', this.handleComponentsChange);
},
this worked after I put it in the correct init :)
The components collection doesn't change, that's why there's no event triggered.
The storage backend is a Backbone collection, see here. You have to listen to its events, specifically add and remove.
As already mentioned by Andreas, you have to add a listener to the collection.
init() {
const children = this.model.components();
this.listenTo(children, 'add remove', this.handleComponentsChange);
},
Related Questions and Answers
Continue research with similar issue discussions.
Issue #828
[QUESTION] Components 'add' event fires different number of times
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 compo...
Issue #2404
Question: how to observe children changes in component
Is there an easy way to subscribe/listen to changes in the components/children of my custom component type? Perhaps an event I can subscrib...
Issue #2839
[Question] I added one component named template in the blocks but when i try to drag another components inside this template component ,it will be dragged but after saving it removes all the components inside it. How to avoid this?
This is code of template component: I debugged it, so i got that : setComponents method in the dom_components clearing it ...but not gettin...
Issue #639
Re-render Component on Canvas when tagName has changed
I'm trying to build a basic Header component that lets you select H1 to H6 with a trait. But when an option is selected, the canvas doesn't...
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.