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

时间:2016-03-10 10:44:15

标签: javascript backbone.js marionette

这是一个儿童视图 - 木偶项目视图,由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!!!
  }
});

2 个答案:

答案 0 :(得分:3)

您应该可以像以下一样调用它:

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

来自CompositeView的

this.children.apply("selectAll",[1,2]);

您可以在backbone.babysitter找到更多信息,这是木偶用于处理此问题的

答案 1 :(得分:1)

我更喜欢迭代孩子:

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