2015-08-28 179 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提供的函數),並從你的任何路由中使用它。 –

相關問題