Meteor publish/subscribe strategies for unique client-side collections

后端 未结 3 1774
故里飘歌
故里飘歌 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 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

提交回复
热议问题