如何从父视图调用子视图方法?

How to call child View method from parent View?

这是一个子视图 - Marionette ItemView,父视图由 Marionette CompositeView 呈现! 我尝试使用 Backbone.babysitter,但没有结果

var CheckboxView = Marionette.ItemView.extend({
  template: JST["components-checkboxItem"],
  className: "checkbox",
  ui: {
    "checkbox": "#checkbox-item"
  },

  selectAll: function () {
    //do some stuff here (this method should be called from parent)
  }
});

module.exports = Marionette.CompositeView.extend({
  className: 'multiselect',
  template: JST["components-multiselect"],
  childView: CheckboxView,
  childViewContainer: ".checkboxes",

  events: {
    "click .selectAll": "selectAll",
  },
  
  selectAll: function () {
    //I need to call appropriate child method from here!!!
  }
});

您应该可以像这样调用它:

this.children.call("selectAll",1,2);

this.children.apply("selectAll",[1,2]); 来自 CompositeView。

您可以在 backbone.babysitter 找到更多信息,marionette 正在使用它来处理此问题

我更喜欢迭代子代:

this.children.each(function (itemView) {
    itemView.selectAll(args)
});