本文介绍了流星使用获取还是在模板帮助器函数中查找?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我返回findfetch的结果,在流星模板帮助器函数内部,性能,重渲染次数或其他任何内容是否存在差异?

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();
};

仅查找"方法是文档中的当前方法,但是我看到很多其他人使用fetch.

The find-only approach is what is currently in the docs, but I’ve seen lots of other people using 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)

这篇关于流星使用获取还是在模板帮助器函数中查找?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 02:08