Publications and Subscriptions

תפריט צד 4.5

אחוז תורגם

בפרק זה:

  • Understand how publications and subscriptions work.
  • Learn what the default Autopublish package does.
  • See a few more examples of publication patterns.
  • ////

    ////

    ////

    The Olden Days

    ////

    ////

    ////

    ////

    ////

    The Meteor Way

    ////

    Pushing a subset of the database to the client.
    Pushing a subset of the database to the client.

    ////

    ////

    ////

    Publishing

    ////

    ////

    ////

    All the posts contained in our database.
    All the posts contained in our database.

    ////

    ////

    Excluding flagged posts.
    Excluding flagged posts.

    ////

    // on the server
    Meteor.publish('posts', function() {
      return Posts.find({flagged: false}); 
    });
    

    ////

    DDP

    ////

    ////

    Subscribing

    ////

    ////

    ////

    Subscribing to Bob's posts will mirror them on the client.
    Subscribing to Bob’s posts will mirror them on the client.

    ////

    // on the server
    Meteor.publish('posts', function(author) {
      return Posts.find({flagged: false, author: author});
    });
    

    ////

    // on the client
    Meteor.subscribe('posts', 'bob-smith');
    

    ////

    Finding

    ////

    Selecting a subset of documents on the client.
    Selecting a subset of documents on the client.

    ////

    // on the client
    Template.posts.helpers({
      posts: function(){
        return Posts.find(author: 'bob-smith', category: 'JavaScript');
      }
    });
    

    ////

    Autopublish

    ////

    ////

    Autopublish
    Autopublish

    ////

    ////

    ////

    Publishing Full Collections

    ////

    Meteor.publish('allPosts', function(){
      return Posts.find();
    });
    
    Publishing a full collection
    Publishing a full collection

    ////

    Publishing Partial Collections

    ////

    Meteor.publish('somePosts', function(){
      return Posts.find({'author':'Tom'});
    });
    
    Publishing a partial collection
    Publishing a partial collection

    Behind The Scenes

    ////

    ////

    ////

    ////

    • ////
    • ////
    • ////

    ////

    Publishing Partial Properties

    ////

    ////

    Meteor.publish('allPosts', function(){
      return Posts.find({}, {fields: {
        date: false
      }});
    });
    
    Publishing partial properties
    Publishing partial properties

    ////

    Meteor.publish('allPosts', function(){
      return Posts.find({'author':'Tom'}, {fields: {
        date: false
      }});
    });
    

    Summing Up

    ////

    ////

    ////