Javascript Meteor JS iron路由器:在路由之间共享公共“数据”
目前,我的路由器是这样的:Javascript Meteor JS iron路由器:在路由之间共享公共“数据”,javascript,meteor,iron-router,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.f
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
};
如何避免在不同路径中重复代码?
理想情况下,我希望在一个地方,许多路线可以使用它。
这样,如果我决定在重复的代码中做一些小的更改,我就不需要在许多不同的地方进行更改。。。。
我该怎么做
我没有使用RouteControl的原因是,对于某些路由,我需要添加更多的数据,以便在路由器的数据功能中返回……但也许我不知道如何使用RouteControl来解决此类问题
如何清理上面的代码?您可以使用以下代码:
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
};
}
}
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'
});
注意:这是示例代码,没有看到您的实际路线和模板。是,这是使用RouteControl的标准方法。但我想我的问题是…假设我得到了另一个名为“AnotherBirth”的路由,它使用BirthController中的相同数据加上更多数据…你如何在不影响使用BirthController但不需要“AnotherBirth”使用的“AnotherBirth”数据的/Birth路由的情况下向BirthController添加“AnotherBirth”数据?你可以使用_扩展或超级,请参阅@ilrein的答案在控制器和扩展方面是正确的。请注意,您还可以在router.js文件中定义一个js函数,例如@thenchanter提供的函数,并在任何路由中使用它,我建议将绑定到模板的任何数据移动到模板级订阅中,避免数据管理和iron路由器控制器一起使用。