Meteor:集合、变量、发布和订阅的名称之间的区别? [英] Meteor: difference between names for collections, variables, publications, and subscriptions?

查看:31
本文介绍了Meteor:集合、变量、发布和订阅的名称之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Discover Meteor 示例中,帖子"和帖子"之间的区别是什么?为什么当我们从服务器插入时我们使用posts",而当从浏览器查询时我们使用Posts"?系统不会被大小写差异混淆吗?

In the Discover Meteor examples, what's the diff between "posts" and "Posts"? Why is it that when we do an insert from the server we use "posts" but when querying from the browser we use "Posts"? Wouldn't the system be confused by the case differences?

我在posts.js 中看到了客户端帖子到服务器帖子的变量分配.将客户端大写并使用小型大写字母表示服务器是一种传统的表示法吗?

I see the variable assignment for client Posts to the server posts in posts.js. Is it a conventional notation to capitalize client and use small caps for server?

Posts = new Meteor.Collection('posts')

为什么 server/fixtures.js 使用Posts"?我假设我们在浏览器(客户端)中查询帖子",并在服务器中使用帖子",就像我们在流星 mongo 中所做的那样.那么为什么我们现在在服务器中使用 Posts?

Why does server/fixtures.js use "Posts"? I was under the assumption that we query "Posts" in the browser (client), and use "posts" in the server, like we did in meteor mongo. So why are we now using Posts in the server?

推荐答案

让我们区分一下在编写 Meteor 时可能需要处理的不同名称:

Let's distinguish between the different names you might have to deal with when programming Meteor:

  • 变量名,例如Posts = new Meteor.Collection(...).这些仅用于让您的代码知道如何访问此变量.Meteor 不知道也不关心它是什么,尽管约定俗成是大写.
  • 集合名称,例如new Meteor.Collection("posts").这映射到 MongoDB 集合(在服务器上)或 minimongo 集合(在客户端上)的名称.
  • 发布和订阅名称,用于 Meteor.publish("foo", ...)Meteor.subscribe("foo")代码>.这些必须匹配才能让客户端订阅服务器上的某些数据.
  • Variable names, such as Posts = new Meteor.Collection(...). These are used only so your code knows how to access this variable. Meteor doesn't know or care what it is, although the convention is to capitalize.
  • Collection names, such as new Meteor.Collection("posts"). This maps to the name of a MongoDB collection (on the server) or a minimongo collection (on the client).
  • Publication and subscription names, used in Meteor.publish("foo", ...) or Meteor.subscribe("foo"). These have to match up for the client to subscribe to some data on the server.

您需要在 Meteor 数据模型中匹配两件事:

There are two things you need to match up in the Meteor data model:

  1. 出版物名称及其相应订阅
  2. (通常) 客户端和服务器上的集合名称(如果使用默认集合模型)
  1. Names of publications and their corresponding subscriptions
  2. (usually) Names of collections on the client and server, if using the default collection model

订阅名称需要始终与发布名称匹配.但是,为给定订阅发送的集合与订阅名称没有任何关系.事实上,可以发送一个出版物中的多个游标不同出版物中的一个集合,甚至每个出版物中的多个订阅,它们合并为一个在客户端.您还可以在服务器和客户端使用不同的集合名称;继续阅读...

A subscription name needs to always match up with the name of a publication. However, the collections that are sent for a given subscription needn't have anything to do with the subscription name. In fact, one can send over multiple cursors in one publication or one collection over different publications or even multiple subscriptions per publication, which appear merged as one in the client. You can also have different collection names in the server and client; read on...

让我们回顾一下不同的情况:

Let's review the different cases:

  1. 简单的订阅模式.这是您通常在简单的 Meteor 演示中看到的.

  1. Simple subscription model. This is the one you usually see in straightforward Meteor demos.

在客户端和服务器上,

Posts = new Meteor.Collection("posts");

仅在服务器上:

Meteor.publish("postsPub", function() { 
    return Posts.find() 
});

仅限客户端:

Meteor.subscribe("postsPub")

这会使用名为 postsPub 的发布同步 Posts 集合(在数据库中名为 posts).

This synchronizes the Posts collection (which is named posts in the database) using the publication called postsPub.

一个出版物中有多个收藏.您可以使用数组为单个发布发送多个游标.

Multiple collections in one publication. You can send multiple cursors over for a single publication, using an array.

在客户端和服务器上:

Posts = new Meteor.Collection("posts");
Comments = new Meteor.Collection("comments");

仅在服务器上:

Meteor.publish("postsAndComments", function() { 
    return [ 
        Posts.find(), 
        Comments.find() 
    ]; 
});

仅限客户端:

Meteor.subscribe("postsAndComments");

这会使用名为 postsAndComments 的单个发布同步 Posts 集合以及 Comments 集合.这种类型的发布非常适合关系数据;例如,您可能只想发布某些帖子以及仅与这些帖子相关的评论.请参阅一个可以自动构建这些游标的包.

This synchronizes the Posts collection as well as the Comments collection using a single publication called postsAndComments. This type of publication is well-suited for relational data; for example, where you might want to publish only certain posts and the comments associated only with those posts. See a package that can build these cursors automatically.

一个集合的多个出版物.您可以使用多个发布为单个集合发送不同的数据切片,这些数据由 Meteor 自动合并.

Multiple publications for a single collection. You can use multiple publications to send different slices of data for a single collection which are merged by Meteor automatically.

在服务器和客户端:

Posts = new Meteor.Collection("posts");

仅在服务器上:

Meteor.publish("top10Posts", function() { 
    return Posts.find({}, {
        sort: {comments: -1}, 
        limit: 10
    });
});        
Meteor.publish("newest10Posts", function() { 
    return Posts.find({}, {
        sort: {timestamp: -1},
        limit: 10
    }); 
});

仅限客户端:

Meteor.subscribe("top10Posts");
Meteor.subscribe("newest10Posts");

这会将网站上评论最多的 10 个帖子和 10 个最新帖子推送给用户,用户会看到这两组数据合并到一个 Posts 集合中.如果最新帖子之一也是评论最多的帖子,反之亦然,帖子 集合将包含少于 20 个项目.这是 Meteor 中的数据模型如何让您无需自己实现细节即可执行强大的数据合并操作的示例.

This pushes both the 10 posts with the most comments as well as the 10 newest posts on the site to the user, which sees both sets of data merged into a single Posts collection. If one of the newest posts is also a post with the most comments or vice versa, the Posts collection will contain less than 20 items. This is an example of how the data model in Meteor allows you to do powerful data merging operations without implementing the details yourself.

每个出版物有多个订阅.您可以使用不同的参数从同一出版物中获取多组数据.

Multiple subscriptions per publication. You can get multiple sets of data from the same publication using different arguments.

在服务器和客户端:

Posts = new Meteor.Collection("posts");

仅在服务器上:

Meteor.publish("postsByUser", function(user) { 
    return Posts.find({
        userId: user
    });
});        

仅限客户端:

Meteor.subscribe("postsByUser", "fooUser");
Meteor.subscribe("postsByUser", "barUser");

这会导致 fooUserbarUser 的帖子都显示在 posts 集合中.当您有多个不同的计算查看数据的不同切片并且可能会动态更新时,此模型很方便.请注意,当您在 Deps.autorun(...), Meteor 会自动调用任何先前具有相同名称的订阅句柄的 stop(),但如果您在 autorun 之外使用这些订阅,则您需要自己停止它们.截至目前,您不能在 autorun 计算中执行两个具有相同名称的订阅,因为 Meteor 无法区分它们.

This causes the posts by fooUser and barUser to both show up in the posts collection. This model is convenient when you have several different computations that are looking at different slices of your data and may be updated dynamically. Note that when you subscribe inside a Deps.autorun(...), Meteor calls stop() on any previous subscription handle with the same name automatically, but if you are using these subscriptions outside of an autorun you will need to stop them yourself. As of right now, you can't do two subscriptions with the same name inside an autorun computation, because Meteor can't tell them apart.

通过发布推送任意数据.您可以完全自定义发布,不需要服务器和客户端上的集合名称相同.事实上,服务器可以发布完全不受集合支持的数据.为此,您可以使用 API 发布功能.

Pushing arbitrary data over a publication. You can completely customize publications to not require the same collection names on the server and client. In fact, the server can publish data that isn't backed by a collection at all. To do this, you can use the API for the publish functions.

仅在服务器上:

Posts = new Meteor.Collection("posts"); 

Meteor.publish("newPostsPub", function() {
    var sub = this;
    var subHandle = null;

    subHandle = Posts.find({}, {
        sort: {timestamp: -1},
        limit: 10
    })
    .observeChanges({
        added: function(id, fields) {
            sub.added("newposts", id, fields);            
        },
        changed: function(id, fields) {
            sub.changed("newposts", id, fields);            
        },
        removed: function(id) {
            sub.removed("newposts", id);
        }
    });

    sub.ready();

    sub.onStop(function() {
        subHandle.stop();
    })    
});

仅限客户端:

NewPosts = new Meteor.Collection("newposts");

Meteor.subscribe("newPostsPub");

这会将服务器上 Posts 集合(在数据库中称为 posts)中的最新 10 个帖子同步到服务器上的 NewPosts 集合客户端(在 minimongo 中称为 newposts)使用名为 newPostsPub 的发布/订阅.请注意,observeChangesobserve 不同,后者可以执行一堆其他的东西.

This synchronizes the newest 10 posts from the Posts collection on the server (called posts in the database) to the NewPosts collection on the client (called newposts in minimongo) using the publication/subscription called newPostsPub. Note that observeChanges differs from observe, which can do a bunch of other things.

代码看起来很复杂,但是当您在发布函数中返回一个游标时,这基本上就是 Meteor 在幕后生成的代码.以这种方式编写出版物可以让您更好地控制发送给客户端的内容和不发送的内容.不过要小心,因为您必须手动关闭 observe 句柄并在订阅准备好时进行标记.有关详细信息,请参阅Matt Debergalis 对此过程的描述(但是,该帖子已过期日期).当然,您可以将其与上述其他部分结合起来,以获得非常微妙和复杂的出版物.

The code seems complicated, but when you return a cursor inside a publish function, this is basically the code that Meteor is generating behind the scenes. Writing publications this way gives you a lot more control over what is and isn't sent to the client. Be careful though, as you must manually turn off observe handles and mark when the subscription is ready. For more information, see Matt Debergalis' description of this process (however, that post is out of date). Of course, you can combine this with the other pieces above to potentially get very nuanced and complicated publications.

抱歉这篇文章 :-) 但很多人对此感到困惑,我认为描述所有案例会很有用.

Sorry for the essay :-) but many people get confused about this and I though it would be useful to describe all the cases.

这篇关于Meteor:集合、变量、发布和订阅的名称之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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