Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

meteor cannot observe queries with skip or limit

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

Both of these

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

or

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

result in meteor cannot observe queries with skip or limit

like image 619
Steeve Cannon Avatar asked Apr 14 '12 21:04

Steeve Cannon


1 Answers

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

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

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.

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.

like image 62
debergalis Avatar answered Oct 04 '22 15:10

debergalis