流星 pub/sub 问题
Meteor pub/sub issues
下面是我的 collection 代码
Competitions = new Mongo.Collection("competitions");
var CompetitionsSchema = new SimpleSchema({
year: {
type: String
},
division: {
type : String,
allowedValues: ['Elite', '1st','2nd','3rd','4th','Intro']
},
teams:{
type : [TeamSchema],
allowedValues: (function () {
return Teams.find().fetch().map(function (doc) {
return doc.name;
});
}()) //here we wrap the function as expression and invoke it
}
});
在 allowedValues 函数中
Teams.find is empty.
在路由器中我订阅发布如下
this.route('competitions', {
path: '/admin/competitions',
layoutTemplate: 'adminLayout',
waitOn: function () {
return [
Meteor.subscribe('teams')
];
}
});
这是我的发布功能
Meteor.publish('teams', function() {
return Teams.find({},{sort: {
points: -1,
netRunRate : -1
}});
});
我还需要在其他地方订阅吗?
您的问题出在这段代码中:
allowedValues: (function () {
return Teams.find().fetch().map(function (doc) {
return doc.name;
});
}()) //here we wrap the function as expression and invoke it
这会在页面加载时调用。那时 Teams
集合在客户端仍然是空的。您需要等到数据准备好。由于您在 iron-router 中使用 waitOn
,因此只需将此代码移至 onRendered
回调即可。
下面是我的 collection 代码
Competitions = new Mongo.Collection("competitions");
var CompetitionsSchema = new SimpleSchema({
year: {
type: String
},
division: {
type : String,
allowedValues: ['Elite', '1st','2nd','3rd','4th','Intro']
},
teams:{
type : [TeamSchema],
allowedValues: (function () {
return Teams.find().fetch().map(function (doc) {
return doc.name;
});
}()) //here we wrap the function as expression and invoke it
}
});
在 allowedValues 函数中
Teams.find is empty.
在路由器中我订阅发布如下
this.route('competitions', {
path: '/admin/competitions',
layoutTemplate: 'adminLayout',
waitOn: function () {
return [
Meteor.subscribe('teams')
];
}
});
这是我的发布功能
Meteor.publish('teams', function() {
return Teams.find({},{sort: {
points: -1,
netRunRate : -1
}});
});
我还需要在其他地方订阅吗?
您的问题出在这段代码中:
allowedValues: (function () {
return Teams.find().fetch().map(function (doc) {
return doc.name;
});
}()) //here we wrap the function as expression and invoke it
这会在页面加载时调用。那时 Teams
集合在客户端仍然是空的。您需要等到数据准备好。由于您在 iron-router 中使用 waitOn
,因此只需将此代码移至 onRendered
回调即可。