meteor无法通过跳过或限制来观察查询 [英] meteor cannot observe queries with skip or limit

查看:74
本文介绍了meteor无法通过跳过或限制来观察查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能在文档中遗漏了一些非常明显或遗漏的内容。我搜索过,没有找到类似的问题。发布它。

I am probably missing something quite obvious or missing something in the documentation. I searched and did not find similar question. posting it.

这两个

return Items.find({},{sort: {time: -1}, limit: 10});

return Items.find({},{sort: {time: -1}).limit(10);

结果 meteor无法观察跳过或限制的查询

推荐答案

更新:这是一个问题。从Meteor 0.5.3开始,您可以使用跳过限制选项来观察查询。

UPDATE: This is longer an issue. Beginning with Meteor 0.5.3, you can observe queries with skip and limit options.

不幸的是,这是真的:mimimongo软件包当前不支持在使​​用<$ c $的游标上调用 observe c>跳过或限制选项。这没有充分的理由;它只是没有实现。

Unfortunately, this is true: the mimimongo package doesn't currently support calling observe on cursors that used the skip or limit options. There's no good reason for this; it's just not implemented.

如果您在模板助手中调用此查询,则有一个简单的解决方法:

If you're calling this query inside a template helper, there's an easy workaround:

Template.name.items = function () {
  // fetch array of all the items
  var items = Items.find({}, {sort: {time: -1}}).fetch();

  // return only the first 10 items to the template
  return items.slice(0,10);
};

解决方法的缺点是效率。如果你的助手返回一个游标(只返回 Items.find 的值而不调用 fetch ,那么模板系统是足够聪明,只要一个项目发生变化,或者插入新项目,就不会重新计算整个模板。

The downside of the workaround is efficiency. If your helper returns a cursor (just returning the value of Items.find without calling fetch, then the template system is smart enough not to recalculate the whole template when just one item changes, or if a new item is inserted.

另一方面,调用 fetch帮助器中的注册了对整个查询结果的依赖关系,因此每当查询中的任何对象发生更改时,都会重新计算整个模板。

On the other hand, calling fetch in the helper registers a dependency on the entire query result, so the whole template gets recalculated any time any object in the query changes.

没有其他区别。模板会在屏幕上显示相同的内容,并且在必须重绘时会保留表单元素的内容。

There's no other difference. The template will put the same thing on the screen and it will preserve the contents of form elements when it has to redraw itself.

这篇关于meteor无法通过跳过或限制来观察查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆