在Meteor中,如何只查询给定订阅的记录? [英] In Meteor, how can I query only the records of a given subscription?

查看:73
本文介绍了在Meteor中,如何只查询给定订阅的记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解订阅是一种将记录流到客户端集合中的方法,该信息来自此帖子和其他...

I understand that a a subscription is a way to flow records into a client-side collection, from this post, and others...

但是,根据此帖子,您可以有多个订阅流放入同一集合中.

However, per this post, You can have multiple subscriptions that flow into the same collection.

// 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);

现在-我是通过两个不同的订阅获得记录的,我可以使用订阅来获取它回撤的记录吗?还是我必须重新查询我的收藏集?在客户端和服务器之间共享该查询的最佳实践是什么?

Now - I obtained my records via two different subscriptions, can I use the subscription to get to the records that it pulled back? Or must I requery my collection? What is the best practice for sharing that query between client and server?

我希望我不会在这里遗漏一些明显的东西,但是仅出于副作用执行Meteor.subscribe函数似乎正在丢失一条非常有用的信息-即记录来自哪个订阅.大概选择出版物和订阅的名称是有意义的-如果我能找到与该名称相关联的记录,那就太好了.

I hope I'm not missing something obvious here, but executing the Meteor.subscribe function only for its side-effects seems to be losing a very useful piece of information - namely which subscription a record came from. Presumably the names of publications and subscriptions are chosen to be meaningful - it would be nice if I could get to records associated with that name.

推荐答案

您似乎想要做的是维护两个单独的记录集合,其中每个集合由不同的出版物填充.如果您阅读了 DDP规范,则会看到服务器告诉了客户,每个记录属于哪个集合(而非发布),多个发布实际上可以为同一条记录提供不同的字段.

What you seem to want to do is maintain two separate collections of records, where each collection is populated by a different publication. If you read the DDP specification, you'll see that the server tells the client which collection (not publication) each record belongs to, and multiple publications can actually provide different fields to the same record.

但是,Meteor实际上允许您将记录发送到任意集合名称,客户端将查看它是否具有该集合.例如:

However, Meteor actually lets you send records to any arbitrary collection name, and the client will see if it has that collection. For example:

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());
  });
}

这篇关于在Meteor中,如何只查询给定订阅的记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆