2015-08-28 182 views
0

之间共享常见的“数据”目前,这是我的路由器怎么样子:流星JS铁路由器:路由

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's来解决这类问题....

如何清理上面的代码?

+0

虽然你不会太喜欢这个,但我建议将绑定到你的模板的任何数据转移到模板级订阅中,并避免数据管理和铁路路由器控制器一起使用。 – pushplaybang

回答

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

你可以有一些像这样的代码:

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

注:这是没有看到你的实际路径和模板的示例代码。

+0

是的,这是使用RouteController的标准方式。但我想我的问题是...让我们说我得到另一个名为'anotherbirthday'的路线,它使用BirthController中的相同数据加上一些更多......如何在不影响/生日路线的情况下向BirthController添加“更多”使用BirthController,但不需要“另一个生日”使用的“更多”数据? – preston

+1

您可以使用'_extend'或'__super__',参见http://stackoverflow.com/questions/24223268/ironrouter-extending-data-option-on-route-controller – ilrein

+0

@ ilrein的答案在控制器方面是正确的扩展它们。请注意,你也可以在你的router.js文件中定义一个js函数(例如@thenchanter提供的函数),并从你的任何路由中使用它。 –

相关问题