1

我有一个非常大的数据集,我只发布/订阅其中的一部分。(最后 100 个对象)但是,我还必须返回并访问此数据集的其他切片,但我无法重新发布同名的其他数据集,并且我不希望“非实时”选择成为在所有客户端同步。

推荐的方法是什么?

谢谢,

4

4 回答 4

1

您的发布频道的名称不必与您要发布的集合名称相对应。您可以从一个集合中发布多个集合:

Meteor.publish('currentArticles', function() {
  return Articles.find({}, {limit: 100});
});

Meteor.publish('allArticles', function(chunk) {
  return Articles.find({}, {limit: 100, skip: 100*chunk});
});
于 2013-09-14T21:16:28.267 回答
0

Hubert 的示例将由客户决定调用什么。如果没问题,我没有什么要补充的。但是,如果您需要比这更封闭一些,则需要建立一个带有 userId 列的新集合以公开,

return SlicedArticles.find({_id: Meteor.userId()});

或现有文章对象上的数组以包含有效的用户 ID。

于 2013-09-15T00:47:01.850 回答
0

您可以(并且应该)使用出版物而不是 a 来执行此操作Meteor.method,特别是如果您想要实时更新。

您可以将服务器上的一个集合发布到客户端上的不同集合,一个是“实时”的,一个不是(可能会节省一些资源,例如观察者)。

服务器代码:

Articles = new Meteor.Collection("articles");

// Static articles publication with no observer.
// More efficient than just doing  return Articles.find(); 
Meteor.publish("staticPub", function() {
    var sub = this;
    // Put your arguments in this find() for the "non-live" selection
    Articles.find().forEach(function(article) {
        sub.added("staticArticles", article._id, article);                  
    });

    sub.ready();    
});

// Live latest articles publication
Meteor.publish("latestPub", function() {
    // Only get things at most 30 minutes old
    var cutoff = +new Date - 30 * 60 * 1000;
    return Articles.find({ 
        utctime: {$geq: cutoff} 
    }, {
        limit: 500
    });
});

客户端代码:

StaticArticles = new Meteor.Collection("staticArticles");
Articles = new Meteor.Collection("articles");

Meteor.subscribe("staticPub");
Meteor.subscribe("latestPub");

也可以将最新文章推送到客户端上具有不同名称的集合中,尽管这会使此处的代码更加冗长。有关您可以使用出版物做什么的评论,请参阅https://stackoverflow.com/a/18880927/586086

于 2013-09-18T20:10:31.050 回答
0

这是我实现时间片显示的方法。我没有替换模板生成的表和订阅的数据集,而是创建了一个新表。它有点手动,但在这里。

在服务器上,我只是暴露了一个叫 gethistory 的方法,它取切片的上下限,fetch() 新的数据集,如果切片周期小于 30 分钟则返回取到的数据,否则返回 false:

Meteor.methods({
    gethistory: function(utcfrom, utcto) {
        // If interval is lower than 30 minutes
        if ((utcto - utcfrom) < 1800 * 1000) {
                query = Mon.find({
                    "$and": [{
                        "utctime": {
                            "$gt": (new Date(utcfrom))
                            }   
                    }, {
                        "utctime": {
                            "$lt": (new Date(utcto))
                            }   
                    }]  
                    }, {
                    sort: {
                        utctime: 1
                    },  
                    limit: 500 
                })  
                res = query.fetch()
                return res 
        } else {
            return false
        }   
    }   
})

当我需要显示一个新的数据切片时,我使用来自客户端的以下代码(e.min 和 e.max 包含切片边界)。在我的代码中,这被称为显示一整年的 highchart 的 afterSetExtremes(e) 部分,(如果您对 highchart 部分感兴趣,请查看http://www.highcharts.com/stock/demo/lazy-loading )

Meteor.call('gethistory', e.min, e.max, function(error, data) {
    Session.set('histdata', data)
});

我有一个模板: Template.hist.historylines = function () { return Session.get('histdata') }

还有一个html部分:

<body>
{{> hist}}
</body>

<template name="hist">
  <div>
   {{#if historylines}}
  <table>
    {{#each historylines}}
      {{> line}}
    {{/each}}
   {{else}}
     <p>You must select a period smaller than 30 minutes to get the details.</p>
   {{/if}}
  </table>
  </div>
</template>
<template name="line">
  <tr>
    <td>{{val1}}</td>
    <td>{{val2}}</td>
  </tr>
</template>
于 2013-09-15T09:38:31.903 回答