Meteor publish/subscribe strategies for unique client-side collections

后端 未结 3 1755
故里飘歌
故里飘歌 2020-12-02 08:22

Using Meteor, I\'m wondering how best to handle different client-side collections that share the same server-side database collection. Consider the following example: I hav

相关标签:
3条回答
  • 2020-12-02 08:56

    use publish-composite package

    // main collection
    Entities = new Meteor.Collection('entities');
    
    // partial collections only client side
    RectEntities = new Mongo.Collection('rectEntities');
    PolyEntities = new Mongo.Collection('polyEntities');
    
    // server publish
    Meteor.publishComposite("rectEntities", function(someParameter) {
        return {
                collectionName:'rectEntities',
                find: function() {
                    return Entities.find({shapeType: 'rectangle'});
                },
                children: []
        }
    });
    Meteor.publishComposite("polyEntities", {
            collectionName:'polyEntities',
            find: function() {
                return Entities.find({shapeType: 'polygon'});
            },
            children: []
    });
    

    source : http://braindump.io/meteor/2014/09/20/publishing-to-an-alternative-clientside-collection-in-meteor.html

    0 讨论(0)
  • 2020-12-02 09:03

    In a shared area:

    function getSearchUsers(query) {
      var re = new RegExp(query, "i");
      return Users.find({name: {$regex: re}});
    }
    
    function getFriendUsers() {
      return Users.find({friend: true});    // or however you want this to work
    }
    

    On the server:

    Meteor.publish("searchUsers", getSearchUsers);
    Meteor.publish("friendUsers", getFriendUsers);
    

    On the client:

    Template.search.onCreated(function () {
       var self = this;
       self.autorun(function () {
         self.subscribe("searchUsers", Session.get("searchQuery"));
       });
    });
    
    Template.friends.onCreated(function () {
      this.subscribe("friendUsers");
    });
    
    Template.search.helpers({
      searchResults: function () {
        return getSearchUsers(Session.get("searchQuery"));
      }
    });
    
    Template.friends.helpers({
      results: function () {
        return getFriendUsers();
      }
    });
    

    The key takeaway from this is that what happens behind the scenes when the data is getting transferred over the wire isn't obvious. Meteor appears to combine the records that were matched in the various queries on the server and send this down to the client. It's then up the client to run the same query again to split them apart.

    For example, say you have 20 records in a server-side collection. You then have two publishes: the first matches 5 records, the second matches 6, of which 2 are the same. Meteor will send down 9 records. On the client, you then run the exact same queries you performed on the server and you should end up with 5 and 6 records respectively.

    0 讨论(0)
  • 2020-12-02 09:20

    I'm a little bit late to the party, but there is a way to actually have separate collections on the client for subsets of one server collection. In this example i have a server collection called entities which holds information about polygonsand rectangles.
    Shared code (lib folder):

    // main collection (in this example only needed on the server
    Entities = new Meteor.Collection('entities');
    // partial collections
    RectEntities = new Mongo.Collection('rectEntities');
    PolyEntities = new Mongo.Collection('polyEntities');
    

    Client code:

    // this will fill your collections with entries from the Entities collection
    Meteor.subscribe('rectEntities');
    Meteor.subscribe('polyEntities');
    

    Remember that the name of the subscription needs to match the name of the publication (but not the name of the collection itself)
    Server code:

    Meteor.publish('rectEntities', function(){
        Mongo.Collection._publishCursor( Entities.find({shapeType: 'rectangle'}), this, 'rectEntities'); 
        this.ready();
    });
    
    Meteor.publish('polyEntities', function(){
        Mongo.Collection._publishCursor( Entities.find({shapeType: 'polygon'}), this, 'polyEntities'); 
        this.ready();
    });
    

    Thanks to user728291 for the much simpler solution using _publishCursor()!
    The third argument of the _publishCursor() function is the name of your new collection.
    Source: http://docs.meteor.com/#/full/publish_added

    0 讨论(0)
提交回复
热议问题