【问题标题】:Persisting the order of a meteor publish function on the client在客户端保持流星发布功能的顺序
【发布时间】:2016-09-16 22:42:11
【问题描述】:

我有一个 Meteor 集合,其中包含一个我不想发布给客户端的字段。但是,我希望按此字段对集合进行排序。由于来自服务器的排序顺序不会保留在客户端上,如何实现?

【问题讨论】:

    标签: mongodb sorting meteor publish-subscribe


    【解决方案1】:

    经过一番研究,我找到了解决方案。我在服务器上进行排序并在发布前转换文档以包含“订单”字段。

    在服务器上:

    Meteor.publish('usersOrderedByPrivateField', function () {
       var cursor = Meteor.users.find({}, {sort: {privateField: 1});
       var order = 0;
       var self = this;
       cursor.fetch().forEach(function (doc) {
           doc.order = order++;
           delete doc.privateField;
           self.added("users", doc._id, doc);
       });
       this.ready();
    }
    

    在客户端:

    Meteor.subscribe('usersWithPrivateFieldOrdering');
    Meteor.users.find({},{sort:order:1});
    

    如果有更好的方法,请添加答案或评论。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-25
      • 1970-01-01
      • 1970-01-01
      • 2018-03-20
      • 1970-01-01
      • 1970-01-01
      • 2014-03-15
      • 1970-01-01
      相关资源
      最近更新 更多