之间共享常见的“数据”目前,这是我的路由器怎么样子:流星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