Do backbone marionette.js parent view bubble down?

504 views Asked by At

I reference this question and document I know how to bubble up.

But in my situation, I want to bubble down.

Just like I click a button on parent view then trigger some function of all my childview.

var parent = Marionette.CompositeView.extend({
    triggers: {
        'click #edit': "??"  // trigger the childview
    }
})

above just the code to describe my concept.

Edit

Or maybe not use the marionette, can use backbone to do the trick?

Does anyone know how to make it?

thanks

1

There are 1 answers

0
jfairbank On BEST ANSWER

If you're using Marionette, then you can access all your child views with this.children. The children property delegates some underscore functions like invoke, so you could call this.children.invoke. Something like this might work for your needs:

var ChildView = Marionette.ItemView.extend({
  template: _.template('child'),

  myChildFunction: function() {
    console.log('child view', this);
  }
});

var ParentView = Marionette.CompositeView.extend({
  template: _.template('<button id="edit">Edit</button><div class="children"></div>'),

  childView: ChildView,

  childViewContainer: '.children',

  events: {
    'click #edit': 'triggerChildren'
  },

  triggerChildren: function() {
    this.children.invoke('myChildFunction');
  }
});