我知道订阅是一种将记录流入客户端集合的方法,从这篇帖子和其他人...
但是,根据这篇文章,您可以有多个订阅流入同一个集合.
// server
Meteor.publish('posts-current-user', function publishFunction() {
return BlogPosts.find({author: this.userId}, {sort: {date: -1}, limit: 10});
// this.userId is provided by Meteor - http://docs.meteor.com/#publish_userId
}
Meteor.publish('posts-by-user', function publishFunction(who) {
return BlogPosts.find({authorId: who._id}, {sort: {date: -1}, limit: 10});
}
// client
Meteor.subscribe('posts-current-user');
Meteor.subscribe('posts-by-user', someUser);
Run Code Online (Sandbox Code Playgroud)
现在 - 我通过两个不同的订阅获取了我的记录,我可以使用订阅来获取它撤回的记录吗?或者我必须重新询问我的收藏品吗?在客户端和服务器之间共享该查询的最佳实践是什么?
我希望我不会错过这里显而易见的东西,但Meteor.subscribe仅仅因为它的副作用而执行该功能似乎正在丢失一条非常有用的信息 - 即记录来自哪个订阅.据推测,出版物和订阅的名称被选择为有意义 - 如果我能够获得与该名称相关的记录,那将是很好的.
您似乎想要做的是维护两个单独的记录集合,其中每个集合由不同的发布填充.如果您阅读DDP规范,您将看到服务器告诉客户端每个记录属于哪个集合(而不是发布),并且多个发布实际上可以为同一记录提供不同的字段.
但是,Meteor实际上允许您将记录发送到任意集合名称,客户端将查看它是否具有该集合.例如:
if (Meteor.isServer) {
Posts = new Mongo.Collection('posts');
}
if (Meteor.isClient) {
MyPosts = new MongoCollection('my-posts');
OtherPosts = new MongoCollection('other-posts');
}
if (Meteor.isServer) {
Meteor.publish('my-posts', function() {
if (!this.userId) throw new Meteor.Error();
Mongo.Collection._publishCursor(Posts.find({
userId: this.UserId
}), this, 'my-posts');
this.ready();
});
Meteor.publish('other-posts', function() {
Mongo.Collection._publishCursor(Posts.find({
userId: {
$ne: this.userId
}
}), this, 'other-posts');
this.ready();
});
}
if (Meteor.isClient) {
Meteor.subscribe('my-posts', function() {
console.log(MyPosts.find().count());
});
Meteor.subscribe('other-posts', function() {
console.log(OtherPosts.find().count());
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2809 次 |
| 最近记录: |