Advanced Publications

Ek bilgi bölümü 13.5

Tercüme Yüzdesi

Bu bölümde:

  • Learn more advanced patterns for manipulating publications.
  • See just how flexible publications and subscriptions can get.
  • ////

    Publishing a Collection Multiple Times

    ////

    ////

    ////

    ////

    ////

    Publishing a collection twice
    Publishing a collection twice
    Meteor.publish('allPosts', function() {
      return Posts.find({}, {fields: {title: true, author: true}});
    });
    
    Meteor.publish('postDetail', function(postId) {
      return Posts.find(postId);
    });
    

    ////

    ////

    ////

    ////

    Meteor.publish('newPosts', function(limit) {
      return Posts.find({}, {sort: {submitted: -1}, limit: limit});
    });
    
    Meteor.publish('bestPosts', function(limit) {
      return Posts.find({}, {sort: {votes: -1, submitted: -1}, limit: limit});
    });
    
    server/publications.js

    Subscribing to a Publication Multiple Times

    ////

    ////

    ////

    Subscribing twice to one publication
    Subscribing twice to one publication

    ////

    Meteor.publish('posts', function(options) {
      return Posts.find({}, options);
    });
    

    ////

    Meteor.subscribe('posts', {submitted: -1, limit: 10});
    Meteor.subscribe('posts', {baseScore: -1, submitted: -1, limit: 10});
    

    ////

    ////

    Multiple Collections in a Single Subscription

    ////

    ////

    ////

    ////

    ////

    ////

    ////

    Two collections in one subscription
    Two collections in one subscription
    Meteor.publish('topComments', function(topPostIds) {
      return Comments.find({postId: topPostIds});
    });
    

    ////

    ////

    Meteor.publish('topPosts', function(limit) {
      var sub = this, commentHandles = [], postHandle = null;
    
      // send over the top two comments attached to a single post
      function publishPostComments(postId) {
        var commentsCursor = Comments.find({postId: postId}, {limit: 2});
        commentHandles[post._id] = 
          Meteor.Collection._publishCursor(commentsCursor, sub, 'comments');
      }
    
      postHandle = Posts.find({}, {limit: limit}).observeChanges({
        added: function(id, post) {
          publishPostComments(post._id);
          sub.added('posts', id, post);
        },
        changed: function(id, fields) {
          sub.changed('posts', id, fields);
        },
        removed: function(id) {
          // stop observing changes on the post's comments
          commentHandles[id] && commentHandles[id].stop();
          // delete the post
          sub.removed('posts', id);
        }
      });
    
      sub.ready();
    
      // make sure we clean everything up (note `_publishCursor`
      //   does this for us with the comment observers)
      sub.onStop(function() { postsHandle.stop(); });
    });
    

    ////

    ////

    ////

    Linking different collections

    ////

    One collection for two subscriptions
    One collection for two subscriptions

    ////

    ////

    ////

    ////

      Meteor.publish('videos', function() {
        var sub = this;
    
        var videosCursor = Resources.find({type: 'video'});
        Meteor.Collection._publishCursor(videosCursor, sub, 'videos');
    
        // _publishCursor doesn't call this for us in case we do this more than once.
        sub.ready();
      });
    

    ////

    ////