Meteor JS铁路由器:在路由

时间:2015-08-28 13:57:54

标签: javascript meteor iron-router

目前这就是我的路由器的样子:

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
      };
    }

  });


});

正如您所看到的,代码的重复次数与此类似:

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
      };

如何避免在不同路线中重复代码? 理想情况下,我希望将它放在许多路线可以使用的地方。 这样,如果我决定在重复的代码中进行小的更改,我就不需要在许多不同的地方进行更改.... 我该怎么做?

我没有使用RouteController的原因是因为对于某些路由我需要在路由器的数据功能中添加一些更多的数据.....但也许我只是不知道如何使用RouteController来解决这类问题......

如何清理上面的代码?

2 个答案:

答案 0 :(得分: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
  };
 }
}

答案 1 :(得分:0)

你可以有这样的代码:

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

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

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

则...

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

注意:这是示例代码,但没有看到您的实际路线和模板。