Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'})
            });
});
like image 823
Warz Avatar asked Jun 14 '14 19:06

Warz


2 Answers

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)

like image 168
PhilippSpo Avatar answered Sep 27 '22 22:09

PhilippSpo


To add to the selected answer, note that if you're using Coffeescript the following will yield the same result:

class @ChildController extends ParentController
  data: -> super()
  # or if you want to add to it:
  data: ->
    _.extend super(), { extraThing: 'some value' }
like image 33
Cooper Maruyama Avatar answered Sep 27 '22 22:09

Cooper Maruyama