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'})
});
});
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)
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' }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With