【问题标题】:Is it worth batching subscriptions in Meteor?值得在 Meteor 中批量订阅吗?
【发布时间】:2016-02-05 17:34:23
【问题描述】:

显示文档列表时,在文档级别(相对于列表级别)编写订阅会更清晰,以防模板在其他地方重用。是效率低下还是 Meteor 神奇地处理事情?

更准确地说,我可以将文档逻辑放在文档模板中:

Template.itemsList.helpers({
  items: function() {
    return this.itemsIds.map(function(id) { return { _id: id }; });
  },
});

<template name="itemsList">
  {{#each items}}{{> item}}{{/each}}
</template>


Template.item.onCreated(function() {
  this.subscribe('item', this.data._id);
});

<template name="item">
  {{#if Template.subscriptionsReady}}
    ...
  {{/if}}
</template>

或仅在列表级别订阅这些文档一次:

Template.itemsList.onCreated(function() {
  this.subscribe('items', this.data.itemsIds);
});

Template.itemsList.helpers({
  items: function() {
    return Items.find({_id: {$in: this.itemsIds}});
  },
});

<template name="itemsList">
  {{#if Template.subscriptionsReady}}
    {{#each items}}{{> item}}{{/each}}
  {{/if}}
</template>

<template name="item">
  ...
</template>

看起来第二种方法更有效,因为只有一次调用订阅。如果我们在这里谈论标准的 http 请求,那是毫无疑问的。但是由于 Meteor 是基于套接字的,并且在后台处理了很多事情,所以我想知道将一些逻辑从文档移到列表中是否值得。

【问题讨论】:

    标签: performance meteor publish-subscribe


    【解决方案1】:

    通常最好调用一次订阅。当您使用新参数运行订阅时,发布方法会重新运行,然后它会调用 db 以获取所需的文档并将其发送给客户端。通常只对 db 进行一次调用会更好、更有效。

    但是,如果您确定要显示所有项目并且Items 集合中没有太多项目,则这是有意义的。

    如果有大量项目(数百或更多),最好进行某种分页以避免将所有文档下载到客户端。

    【讨论】:

    • 好吧,我想知道Meteor 是否会将个人订阅分组并与数据库交谈一次,只是为了获得所有订阅,但我知道这将是多么具有挑战性。谢谢
    猜你喜欢
    • 2015-11-24
    • 2015-04-28
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    相关资源
    最近更新 更多