Meteor: difference between names for collections, variables, publications, and subscriptions?

前端 未结 2 592
孤城傲影
孤城傲影 2020-11-28 12:06

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 fro

2条回答
  •  我在风中等你
    2020-11-28 13:01

    You decide the naming conventions, and meteor doesn't care.

    Posts becomes a collection of documents from the mongo server. You find posts by calling Posts.find({author: 'jim}). In the example you wrote, meteor is being told to internally call that collection 'posts'. Hopefully this is easy to remember if the names are similar...

    There needs to be a way to express and track what info is available to clients. Sometimes there may be multiple sets of information, of varying detail. Example: a summary for a title listing, but detail for a particular document. These are often also named 'posts' so it can be initially confusing:

    Meteor.publish "posts", ->  # on server
      Posts.find()  
    

    and then

    dbs.subscriptions.posts = Meteor.subscribe 'posts'  # on client
    

    publication and subscription names must match, but it could all be named like this:

    PostsDB = new Meteor.Collection('postdocumentsonserver')
    

    so in mongo you'd need to type

    db.postdocumentsonserver.find()
    

    but otherwise you never need to care about 'postdocumentsonserver'. Then

    Meteor.publish "post_titles", ->
      PostsDB.find({},{fields:{name:1}})  
    

    matching

    dbs.subscriptions.post_titles = Meteor.subscribe 'post_titles'
    

提交回复
热议问题