Meteor 在模板辅助函数中使用 fetch 或 find 吗? [英] Meteor use fetch or find in template helper functions?
问题描述
在流星模板辅助函数中,如果我返回 find
与 fetch
的结果,在性能、重新渲染次数或其他任何方面是否有任何差异>?
Inside a meteor template helper function, is there any difference in performance, number of re-renders, or anything else if I return the result of a find
vs a fetch
?
例如查找方法:
Template.players.topScorers = function () {
return Users.find({score: {$gt: 100}}, {sort: {score: -1}});
};
或添加提取:
Template.players.topScorers = function () {
return Users.find({score: {$gt: 100}}, {sort: {score: -1}}).fetch();
};
仅查找方法是目前文档 中的内容,但我已经看到很多其他人使用 <代码>获取代码>.
The find-only approach is what is currently in the docs, but I’ve seen lots of other people using fetch
.
推荐答案
是的.
通过使用 fetch,您可以在现场注册对整个查询结果集的依赖.通过使用 find
和稍后使用 {{#each}}
进行迭代,一个依赖被分别注册到每个文档上.所以当一个文档发生变化时,只会重新渲染相关的代码.使用 fetch
时,更改结果集中的任何文档都会重新呈现您使用 fetch
的整个范围.
By using fetch you register a dependency on the entire query result set on the spot. By using find
and later on iterating using {{#each}}
a dependency is registered on every document separately. So when one document changes, only the relevant code is re-rendered. When using fetch
, changing any document in the result-set would re-render the entire scope in which you used fetch
.
对于小的结果集,它没有任何区别.对于频繁更改的较大集合,它可能会减慢计算速度并导致不希望的视觉伪影.
For small result-sets it doesn't make any difference. For larger sets with frequent changes it could slow down computation and cause undesired visual artefacts.
我写了一篇帖子帮助你理解它(虽然它没有直接回答你的问题)
I wrote a post which may help you understand it (it doesn't answer your question directly though)
这篇关于Meteor 在模板辅助函数中使用 fetch 或 find 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!