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

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

问题描述

在发现流星"示例中,帖子"和帖子"之间的区别是什么?为什么当我们在服务器上执行插入操作时使用帖子",而在浏览器中查询时却使用帖子"呢?系统会不会因大小写差异而感到困惑?

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

Posts = new Meteor.Collection('posts')

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

解决方案

让我们区分在对Meteor进行编程时可能要处理的不同名称:

  • 变量名称,例如Posts = new Meteor.Collection(...).这些仅用于使您的代码知道如何访问此变量.流星并不知道或不在乎它是什么,尽管惯例是大写.
  • 集合名称,例如new Meteor.Collection("posts").这将映射到MongoDB集合(在服务器上)或minimongo集合(在客户端上)的名称.
  • 发布和订阅名称,在Meteor.publish("foo", ...)Meteor.subscribe("foo")中使用.这些必须匹配,客户端才能订阅服务器上的某些数据.

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

  1. 出版物名称及其相应的订阅
  2. (通常)(如果使用默认集合模型)在客户端和服务器上的集合名称

订阅名称必须始终与发布名称匹配.但是,为给定订阅发送的集合与订阅名称无关.实际上,一个人可以在一个出版物中发送多个游标,或者在不同出版物上收集一个游标,甚至每个出版物多个订阅,这些游标合并为一个在客户端.您还可以在服务器和客户端中使用不同的集合名称.继续阅读...

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

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

    在客户端和服务器上,

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

    仅在服务器上:

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

    仅在客户端上:

     Meteor.subscribe("postsPub")
     

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

  2. 同一出版物中的多个收藏夹.您可以使用数组为单个发布发送多个光标.

    在客户端和服务器上:

     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集合.这种类型的出版物非常适合关系数据.例如,在您可能只想发布某些帖子以及仅与那些帖子相关联的评论的地方.请参见 可以自动构建这些游标的软件包 .

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

    在服务器和客户端上:

     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集合中.如果最新的帖子之一也是评论最多的帖子,反之亦然,则Posts集合将包含少于20个项目.这是一个示例,说明了Meteor中的数据模型如何使您能够进行强大的数据合并操作而无需自己实现细节.

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

    在服务器和客户端上:

     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无法区分它们.

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

    仅在服务器上:

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

    这使用发布/订阅将最新的10个帖子从服务器上的Posts集合(在数据库中称为posts)同步到客户端上的NewPosts集合(在minimongo中称为newposts). newPostsPub.请注意,observeChanges observe不同,后者可以做很多其他事情. /p>

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

对不起这篇文章:-),但是很多人对此感到困惑,尽管我将描述所有情况都有用.

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?

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')

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?

解决方案

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

  • 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.

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

  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. Simple subscription model. This is the one you usually see in straightforward Meteor demos.

    On client and server,

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

    On server only:

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

    On client only:

    Meteor.subscribe("postsPub")
    

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

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

    On client and server:

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

    On server only:

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

    On client only:

    Meteor.subscribe("postsAndComments");
    

    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.

  3. 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.

    On server and client:

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

    On server only:

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

    On client only:

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

    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.

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

    On server and client:

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

    On server only:

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

    On client only:

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

    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.

  5. 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.

    On server only:

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

    On client only:

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

    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.

    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.

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

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