0

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?

preston
  • 3,721
  • 6
  • 46
  • 78
  • While you're not going to like this too much, I would suggest moving any data bound to your templates into template level subscriptions and avoid data management and iron router controllers all together. – bigmadwolf Jan 28 '16 at 12:46

2 Answers2

0
function get (type) {
var birthInfo = Session.get('data');
var idOfOwner = Meteor.userId()
this.BirthDetails = BirthDetails.find({idOfOwner: idOfOwner}).count();
this.birthInfo: = birthInfo;
return {
 div: this.params.chart,
 birthInfo: birthInfo,
 birthDetails: BirthDetails.find({
 idOfOwner: idOfOwner,
 }),
  hasBirthDetails: hasBirthDetails
  };
 }
}
0

You could have some code like this:

BirthController = RouteController.extend({
  waitOn: function () { return Meteor.subscribe('birth', idOfOwner); },

  data: function () { return BirthDetails.findOne({_id: idOfOwner}) },

  action: function () {
    this.render();
  }
});

Then...

Router.route('/birthday, {
  name: 'birth.details',
  controller: 'BirthController'
});

Note: this is sample code without seeing your actual routes and templates.

ilrein
  • 3,833
  • 4
  • 31
  • 48
  • Yes that is the standard way of using RouteController. But I guess my question is...let's say I got another route called 'anotherbirthday' that uses the same data in BirthController plus some more...how do u add that 'some more' to BirthController without affecting the /birthday route which uses BirthController but does not need the 'some more' data that 'another birthday' uses? – preston Aug 28 '15 at 14:39
  • 1
    You can use either `_extend` or `__super__`, see http://stackoverflow.com/questions/24223268/ironrouter-extending-data-option-on-route-controller – ilrein Aug 28 '15 at 14:55
  • @ilrein's answer is correct in terms of controllers and extending them. Note that you can also define a js function (such as the one provided by @thenchanter) within your router.js file and use it from any of your routes. – Michel Floyd Aug 30 '15 at 05:20