Can I inherit and extend data from a parent controller?


#1

I have a controller for “Departments”, which is the parent to “Teachers”. I have a controller in order to find the department ahead of time and do a few actions beforehand.

var DepartmentController = RouteController.extend({
  waitOn: function () {
    return Meteor.subscribe('userDepartments');
  },
  onBeforeAction: function () {
    if(!this.params.departmentSlug)
      this.stop();
    var department = Departments.findOne({ slug: this.params.departmentSlug});
    if(!department) {
      this.render('error', {
        data: { message: "You cannot view this department" }
      })
    } else {
      // I want to pass my "department" to the inherited controller.
      this.next();
    }
  }
});

In my Router.map for my other controller, I have the following.

Router.map(function () {
  this.route('Teacher.list', {
    path: '/department/:departmentSlug/staff/teachers',
    controller: 'TeacherController',  // this extends "DepartmentController"
    action: "showAll",
    waitOn: function () {
      return Meteor.subscribe('teachers', /** need department here */)
    }
  });
});

I also have a controller that is pretty straight forward.

TeacherController = DepartmentController.extend({
  showAll: function () {
    this.render('Teachers.list', {
      data: {
        teachers: Teachers.find()
      }
    })
  }
});

What I really want is to inject the data from the parent controller into the child controller. That is,

{
  teachers: LocalCollection()
  department: { /** some object for department */ }
}

Anyone know how to do this?