Issue #3586Opened July 2, 2021by jcsofts3 reactions

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)

dm-siegelApril 28, 20232 reactions

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 :)

anlumoJuly 8, 20211 reactions

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.

artfJuly 27, 20210 reactions

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.

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.