之間共享常見的「數據」目前,這是我的路由器怎麼樣子:流星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來解決這類問題....
如何清理上面的代碼?
雖然你不會太喜歡這個,但我建議將綁定到你的模板的任何數據轉移到模板級訂閱中,並避免數據管理和鐵路路由器控制器一起使用。 – pushplaybang