Search code examples
inheritancemeteoriron-router

IronRouter extending data option on route controller


Is there a way to extend the data option when using IronRouter and the RouteController, It seems like it gets overridden when I inherit from a super controller, the child controller doesn't extend the defined data properties. I have had similiar issues with the yieldTemplates option on a route and used a workaround (underscore _extends) but it didn't work in this case:

ApplicationController = RouteController.extend({
     data: function(){
          return {
                 user: Meteor.user()   
         }     
   }
});

ChildController = ApplicationController.extend({
  data: function(){
        return {
               // I expect to inherit Meteor.User ?????
               someData: {}
        }
   }
});

EDIT:

After using underscore and the extend function to inherit the prototype function, I am still unable to inherit in route definition's that use the ChildController

this.route('someRoute', {
   template: 'task_template',
   //tasks is not available on the template
   data: function () {
            var base = ChildController.data.call(this);
            console.log(base);
            return _.extend(base, {
                tasks: Tasks.find({state: 'Open'})
            });
});

Solution

  • I use something similar to this in a production app:

    Router.route('/test/:testparam', {
        name: 'test',
        controller: 'ChildController'
    });
    
    ParentController = RouteController.extend({
        data: function() {
            console.log('child params: ', this.params);
            return {
                foo: 'bar'
            };
        }
    });
    
    ChildController = ParentController.extend({
        data: function() {
            var data = ChildController.__super__.data.call(this);
            console.log(data);
            return data;
        }
    });
    

    Using __super__ seems to do the trick!
    You can than use _.extend to extend the data (http://underscorejs.org/#extend)