Meteor.js:在一个集合上有多个订阅,在一个本地集合中强制存储结果(解决方案?)



是否有一种方法可以在不同的minimongo集合中存储相同服务器集合的订阅?

如果没有,是否有最佳实践可以解决?

我确实有一个汇总表,其中有50k个数据集,文档中有很多细节。

// Server
var collection = new Meteor.Collection("collection");
Meteor.publish("detail", function (id) {
   return collection.find({_id: id});
});
// A pager that does not include the data (fields:{data:0})
Meteor.publish("master", function (filter, sort, skip, limit) {
   return collection.find({name: new RegExp("^" + filter + "|\s" + filter, "i")}, {limit: limit, skip: skip, sort: options, fields: {data: 0}});
});
// Client
var collection = new Meteor.Collection("collection");
Deps.autorun(function () {
  Meteor.subscribe("master",
      Session.get("search"),
      Session.get("sort"),
      Session.get("skip"),
      Session.get("limit")
  );
  Meteor.subscribe("detail", Session.get("selection"));
});

上面的问题:两个订阅都是同一个集合的提要。

如果查找结果存储在相同的本地集合中,则此方法不能很好地工作。

拥有一个带有订阅/发布名称的本地集合将是非常棒的。

// Client
var detail = new Meteor.Collection("detail"),
    master = new Meteor.Collection("master");

任何想法?

如果您希望您的客户端集合具有与服务器端集合不同的名称,您不能只是返回集合游标。这可以在publish函数中完成,像这样:

Meteor.publish("details", function (id) {  //details here matches the subscribe request
  var self = this;
  self.added( "details", id, collection.findOne({_id: id});  //details here tells the client which collection holds the data
  self.ready();
});

这不会是反应性的,但可以通过在http://docs.meteor.com的counts by room示例中使用observe来实现,这里详细解释了Meteor docs中的messages-count示例是如何工作的?

虽然这回答了您的问题,如何获得一个集合的特定名称,而没有在服务器上的集合。我认为你可能更容易得到你想要的发布函数,更像这样:

Meteor.publish("master", function (filter, sort, skip, limit, id) {
  return [ 
    collection.find({name: new RegExp("^" + filter + "|\s" + filter,     "i")}, {limit: limit, skip: skip, sort: options, fields: {data: 0}})
    , collection.find( id , {fields: {data: 1}} )
    ];
});

然后订阅客户端:

Deps.autorun(function () {
  Meteor.subscribe("master",
    Session.get("search"),
    Session.get("sort"),
    Session.get("skip"),
    Session.get("limit"),
    Session.get("selection")
  );
});

然后,即使您的所有数据都在一个集合中,您也可以使用响应游标指向包含数据的选定id。来自客户端的查询,如下所示:

collection.find( Session.get("selection") );

最新更新