【发布时间】: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