Currently this is how my router looks like:
Router.map(function(){
this.route('Home',{
path:'/',
template:'home',
waitOn: function(){
},
data: function(){
if(Meteor.userId()){
var idOfOwner = Meteor.userId()
var count = BirthDetails.find({idOfOwner: idOfOwner}).count();
var hasBirthDetails;
if(count > 0){
hasBirthDetails = true;
}else{
hasBirthDetails = false;
}
}
return {
birthDetails: BirthDetails.find({
idOfOwner: idOfOwner,
}),
hasBirthDetails: hasBirthDetails
};
}
})
this.route('Settings', {
path: '/settings',
template: 'settings',
waitOn: function(){
console.log('settings waitOn');
//return Meteor.subscribe("userData");
},
data: function(){
if(Meteor.userId()){
var idOfOwner = Meteor.userId()
var count = BirthDetails.find({idOfOwner: idOfOwner}).count();
var hasBirthDetails;
if(count > 0){
hasBirthDetails = true;
}else{
hasBirthDetails = false;
}
}
return {
birthDetails: BirthDetails.find({
idOfOwner: idOfOwner,
}),
hasBirthDetails: hasBirthDetails
};
}
});
this.route('Charts', {
path:'/charts/:chart',
template: 'charts',
data: function(){
Session.set("chartToDraw", this.params.chart);
var birthInfo = Session.get('data');
console.log('chart chart chart');
console.log('inside Charts this.params.chart ' + this.params.chart);
console.log('birthInfo');
console.log(birthInfo);
if(Meteor.userId()){
var idOfOwner = Meteor.userId()
var count = BirthDetails.find({idOfOwner: idOfOwner}).count();
var hasBirthDetails;
if(count > 0){
hasBirthDetails = true;
}else{
hasBirthDetails = false;
}
}
return {
div: this.params.chart,
birthInfo: birthInfo,
birthDetails: BirthDetails.find({
idOfOwner: idOfOwner,
}),
hasBirthDetails: hasBirthDetails
};
}
});
this.route('Factors', {
path:'/factors/:factor',
template: 'factors',
data: function(){
console.log('data of factors');
if(Meteor.userId()){
var idOfOwner = Meteor.userId()
var count = BirthDetails.find({idOfOwner: idOfOwner}).count();
var hasBirthDetails;
if(count > 0){
hasBirthDetails = true;
}else{
hasBirthDetails = false;
}
}
var factorToDisplay = this.params.factor;
console.log(factorToDisplay);
var factorData = Session.get(factorToDisplay);
console.log(factorData);
var hasFactorData;
if(typeof factorData === 'undefined'){
}else{
hasFactorData = true;
}
return {
hasFactorData : hasFactorData,
factor: this.params.factor,
factorData : factorData,
hasBirthDetails: hasBirthDetails,
birthDetails: BirthDetails.find({
idOfOwner: idOfOwner,
}),
}
}
});
this.route('Data', {
path: '/data',
template: 'data',
waitOn: function(){
//return [Meteor.subscribe("name", argument);]
//return [Meteor.subscribe("birth_details")];
},
data: function(){
if(Meteor.userId()){
var idOfOwner = Meteor.userId()
var count = BirthDetails.find({idOfOwner: idOfOwner}).count();
var hasBirthDetails;
if(count > 0){
hasBirthDetails = true;
}else{
hasBirthDetails = false;
}
}
return {
birthDetails: BirthDetails.find({
idOfOwner: idOfOwner,
}),
hasBirthDetails: hasBirthDetails
};
}
});
});
As you can see there is a few repetition of the code that is similar to this:
if(Meteor.userId()){
var idOfOwner = Meteor.userId()
var count = BirthDetails.find({idOfOwner: idOfOwner}).count();
var hasBirthDetails;
if(count > 0){
hasBirthDetails = true;
}else{
hasBirthDetails = false;
}
}
return {
birthDetails: BirthDetails.find({
idOfOwner: idOfOwner,
}),
hasBirthDetails: hasBirthDetails
};
How do I avoid repeating code in different routes? Ideally I would like to have it in one place that many routes can use. That way I don't need to change in many different places if I decide to make small changes in the repeated code.... How do I do this?
The reason why I have not used a RouteController is because for some of the routes I would need to add some more data to be returned in the data function of the router.....but maybe I just don't know how to use RouteController's to solve this kind of issue....
How do I clean up the code above?