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 co
I think _.extends
should work in this case as well:
ChildController = ApplicationController.extend({
data: function() {
var base = ApplicationController.data.call(this);
return _.extends(base, {
someData: {},
});
}
});
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' }
Another option that may achieve the same result is to define a method on your parent controller, then call it using super without extending anything. It's slightly more work for each controller but is easier to apply retroactively. Also it makes the method optional for your child controller rather than by default.
ApplicationController = RouteController.extend({
waitOn: function() {
return [Meteor.subscribe('customUserPublish')];
},
GetProfileWithEmail: function(){
var user = Meteor.user();
var profile = user.profile;
profile.email = user.emails[0].address;
return profile;
}
});
ProfileController = ApplicationController.extend({
waitOn: function() {
return [Meteor.subscribe('anotherCollectionPublish')];
},
data: function(){
return {
profile: function(){
var profile = ApplicationController.__super__.GetProfileWithEmail.call(this);
console.log('profile from super', profile);
return profile;
}
}
}
});
Remember that you have to subscribe to the published collection as well and I believe that you need to use the waitOn array option so that it will merge the subs properly (admittedly I always use an array format so YMMV). You can access the data in your template using {{#with profile}}...{{/with}} or if you were getting back an array of objects use {{#each profile}}...{{/each}}.
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)